简体   繁体   English

在React Native中设置状态不起作用

[英]setting state in React Native doesn't work

I am trying to set the state of my Screen to some values that I need to send to a database afterwards. 我试图将“屏幕”的状态设置为一些以后需要发送到数据库的值。 These values come from a form and are the correct values. 这些值来自表格,是正确的值。 Now I have the issue that the setState function seems to get ignored... 现在我有一个问题,就是setState函数似乎被忽略了...

I have tried this code: 我已经试过这段代码:

handleSubmit = () => {
    const value = this._form.getValue();

    console.log(value);

    this.setState({
        description: value.description,
        places_free: value.places_free
    })
    console.log(this.state);
}

the console logs for this look as followed lines of code show: 控制台日志显示如下:

Struct {
  "description": "Test description",
  "phone": "09 353 90 50",
  "places_free": 2,
  "website": "http://www.oakgent.be/",
}

Object {
  "description": "",
  "latitude": 51.055979,
  "longitude": 3.711001,
  "name": "Oak Restaurant",
  "phone": "09 353 90 50",
  "places_free": null,
  "website": "http://www.oakgent.be/",
}

I also tried by setting the variables myself in the setState function to see if it has to do with the struct but that gives the same result: 我还尝试通过自己在setState函数中设置变量来查看它是否与struct有关,但这给出了相同的结果:

handleSubmit = () => {
    const value = this._form.getValue();

    console.log(value);

    this.setState({
        description: "test",
        places_free: 1
    })
    console.log(this.state);
}

console logs: 控制台日志:

Struct {
  "description": "Test description",
  "phone": "09 353 90 50",
  "places_free": 2,
  "website": "http://www.oakgent.be/",
}

Object {
  "description": "",
  "latitude": 51.055979,
  "longitude": 3.711001,
  "name": "Oak Restaurant",
  "phone": "09 353 90 50",
  "places_free": null,
  "website": "http://www.oakgent.be/",
}

I am kinda stuck at this point and I was hoping someone could lend me a hand 我有点卡在这一点上,我希望有人可以帮我

setState is async doing console.log(this.state); setState是异步执行console.log(this.state); will return the old data. 将返回旧数据。

setState() does not immediately mutate this.state but creates a pending state transition. setState()不会立即使this.state发生突变,但会创建一个挂起的状态转换。 Accessing this.state after calling this method can potentially return the existing value. 调用此方法后访问this.state可能会返回现有值。 There is no guarantee of synchronous operation of calls to setState and calls may be batched for performance gains. 无法保证对setState的调用的同步操作,并且可能会为提高性能而批量调用。

handleSubmit = () => {
  const value = this._form.getValue();

  console.log(value);

  this.setState({
    description: "test",
    places_free: 1
  }, () => console.log(this.state))

}

DEMO 演示

 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script> <div id="root"></div> <script type="text/babel"> class App extends React.Component { constructor() { super(); this.state = { name: 'React', modalShow: false }; } render() { return ( <div> <p onClick={() => { console.log(this.state); this.setState({ modalShow: true }, () => { console.log(this.state); }); }}> Calling state with a callback </p> <p onClick={()=> { console.log(this.state); this.setState({ modalShow: true }); console.log(this.state); }}> Priniting state after setting value </p> </div> ); } } ReactDOM.render(<App />, document.getElementById('root')); </script> 

您需要在setState操作中控制台日志,因为setState是一个同步操作,因此请尝试以下操作:

this.setState({ description: "test", places_free: 1 }, () => console.log(this.state))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM