简体   繁体   English

功能 setState 在 REACT 中不起作用

[英]Functional setState doesn't work in REACT

My attempts to change the value of the state "id" with setState doesn't work.我尝试使用 setState 更改 state“id”的值不起作用。 I've tried using ordinary setState, which doesn't update the value of the state.我试过使用普通的 setState,它不会更新 state 的值。 So I tried a functional setState, but it still doesn't work.所以我尝试了一个功能性的setState,但它仍然不起作用。 The alert shows 0, instead of 5.警报显示 0,而不是 5。

Why is it not updating?为什么不更新?

class Form extends Component {
 constructor(props){
  super(props);
  this.state = {lat: null,
                lng: null,
                radius: null,
                id: 0,
                filename: ''};
  }
  componentDidMount() {
    function setStateFunction(state, props) {
      const newState = {...state, id: 5};
      return newState;
    }
    this.setState(setStateFunction);

    alert(this.state.id);
  }

  render(){
    return (<div>...</div>);
  }
}

Updating a React component's state is asynchronous.更新 React 组件的 state 是异步的。 It does not happen immediately.它不会立即发生。

Since setState is async, the changes might not get reflected as soon as you call it even by passing a function as well.由于setState是异步的,因此即使通过 function 调用它,更改也可能不会立即反映。 If you want to check whether the value is getting updated or not you can check by using the callback of setState result like below.如果要检查值是否正在更新,可以使用setState结果的回调进行检查,如下所示。

componentDidMount() {
    function setStateFunction(state, props) {
      const newState = {...state, id: 5};
      return newState;
    }
    this.setState(setStateFunction, () => {
      alert(this.state.id)
    });
  }

You've got a giant anti-pattern over there.你那里有一个巨大的反模式。 First of all, it does not make sense, to be initializing the state in constructor only to re-initalize it in componentDidMount .首先,在constructor中初始化state只是为了在componentDidMount中重新初始化它是没有意义的。

So unless componentDidMount itself is asynchronous, you should never even be using setState inside it (Even the default configuration by the React team actually throws an error in their eslint-react-plugin if you even attempt to do so - which by the way I strongly recommend you install, it's very good for beginners to help prevent common antipatterns or common mistakes).因此,除非componentDidMount本身是异步的,否则您甚至都不应该在其中使用setState (如果您尝试这样做,即使 React 团队的默认配置实际上也会在他们的eslint-react-plugin中引发错误 - 顺便说一句,我强烈推荐你安装,非常适合初学者帮助防止常见的反模式或常见错误)。

Not to mention, state itself is asynchronous , so it does not update the moment you change it.更何况, state本身是异步的,所以你一改它就不会更新。 Furthermore I presume you're trying to learn here, but just in case this code goes to actual production, you could always simply declare it in constructor此外,我假设您正在尝试在这里学习,但是以防万一此代码用于实际生产,您总是可以简单地在构造函数中声明它

Essentially your code could simply be本质上,您的代码可能只是

class Form extends Component {
  constructor(props) {
    super(props)
    this.state = {
      // rest of your state ...
      id: 5
    }
  }

  componentDidMount() {
    const { id } = this.state
    alert(id)
  }
}

As to the code itself, as I've mentioned, setState is an asynchronous operation, that does update the state instantly.至于代码本身,正如我所提到的, setState是一个异步操作,它会立即更新 state。 One important rule to remember, any time you set state and want to work with the changed value, you should provide a callback要记住的一条重要规则,任何时候设置 state 并希望使用更改后的值,都应该提供回调

setState is defined the following way setState(Function => Object | Object, callbackFunction) setState setState(Function => Object | Object, callbackFunction)如下

So essentially, what you want to be doing in your code is所以本质上,你想在你的代码中做的是

this.setState(setStateFunction, () => alert(this.state.id))

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

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