简体   繁体   English

为什么要使用 React setState updater arguments?

[英]Why use React setState updater arguments?

According to the React.js documentation , when state should be updated based on current state and props, a function should be provided as the first argument to setState , as follows:根据React.js 文档,当 state 应该根据当前的 state 和 props 更新时,一个 function 应该作为setState的第一个参数提供如下:

this.setState((state, props) => {
  return {counter: state.counter + props.step};
});

In contrast, I have been achieving this like so:相比之下,我一直是这样实现的:

this.setState({
  counter: this.state.counter + this.props.step
});

Why is the first method recommended over the second?为什么推荐第一种方法而不是第二种方法?

State updates that depend upon previous state need to reference the previous state to update from. State 更新依赖于之前的 state需要参考之前的 state 进行更新。 With the functional state update you can correctly access previous state.通过功能性 state 更新,您可以正确访问以前的 state。

setState设置状态

setState() does not always immediately update the component. setState()并不总是立即更新组件。 It may batch or defer the update until later.它可能会批量更新或将更新推迟到以后。 This makes reading this.state right after calling setState() a potential pitfall.这使得在调用setState()之后立即阅读this.state是一个潜在的陷阱。 Instead, use componentDidUpdate or a setState callback ( setState(updater, callback) ), either of which are guaranteed to fire after the update has been applied.相反,请使用componentDidUpdatesetState回调 ( setState(updater, callback) ),它们都保证在应用更新后触发。

With the regular update随着定期更新

this.setState({
  counter: this.state.counter + this.props.step
});

Multiple calls to update state during the same render cycle will all reference the current state value from the current render cycle, ie state hasn't updated yet for any subsequent calls within the cycle.同一渲染周期内多次调用更新 state 都将引用当前渲染周期中的当前 state 值,即 state 尚未更新周期内的任何后续调用。 All the updates still occur, however, they each independently overwrite state, so the last update is the one that really updates state.所有更新仍然发生,但是,它们各自独立地覆盖 state,因此最后一次更新是真正更新 state 的更新。

Consider this demo of regular and functional state updates考虑这个常规和功能 state 更新的演示

/**
 * count +3 click handler using naive state updates.
 */
clickHandler1 = () => {
  // assume count equals some number n
  this.setState({ count: this.state.count + 1 }); // update queued, count === n, count = n + 1
  this.setState({ count: this.state.count + 1 }); // update queued, count === n, count = n + 1
  this.setState({ count: this.state.count + 1 }); // update queued, count === n, count = n + 1
  // when processed the count will be n + 1
};

/**
 * count +3 click handler using functional state updates.
 */
clickHandler2 = () => {
  // assume count equals some number n
  this.setState((state, props) => ({ count: state.count + 1 })); // update queued, count === n + 0, count = prevCount + 1
  this.setState((state, props) => ({ count: state.count + 1 })); // update queued, count === n + 1, count = prevCount + 1
  this.setState((state, props) => ({ count: state.count + 1 })); // update queued, count === n + 2, count = prevCount + 1
  // now when processed each call uses the result of the previous update
  // count will be n + 1 + 1 + 1, or n + 3
};

编辑反应 - 基于类的常规和功能状态更新

Because this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state.因为 this.props 和 this.state 可能会异步更新,所以您不应依赖它们的值来计算下一个 state。

Read the document from React.js从 React.js 读取文档

https://reactjs.org/docs/state-and-lifecycle.html https://reactjs.org/docs/state-and-lifecycle.html

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

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