简体   繁体   English

反应 - onChange 事件 / setState

[英]React - onChange event / setState

I tried to make a filter items functionality and I encountered the following code:我尝试制作过滤器项目功能,但遇到以下代码:

handleChange = e => this.setState({ [e.target.data.inputType]: e.target.value }, this.filterItems);

What is the purpose of the second argument filterItems when I use setState ?当我使用setState时,第二个参数filterItems的目的是什么?

Think of setState as an asynchronous operation, if you need to do something after the state changes you should put it in the second parameter callback.setState看成一个异步操作,如果你需要在 state 改变后做一些事情,你应该把它放在第二个参数回调中。

In this example the code inside the condition may not be reached:在这个例子中,条件中的代码可能无法到达:

// Assuming isReady initial value is false.
this.setState({
 isReady: true
});

//At this moment this.state.isReady is probably still falsy.
if (this.state.isReady) { ... }

To be sure, you need to pass the code in the (After state) callback.可以肯定的是,您需要在 (After state) 回调中传递代码。 With this, you guarantee your code will only be executed once the new state takes effect.有了这个,您可以保证您的代码只会在新状态生效后才会执行。

// Assuming isReady initial value is false.
this.setState({
 isReady: true
}, () => {
  // React calls this function when the updates are applied.
  // Which means we are sure isReady have the new value.
  if (this.state.isReady) { ... }
});

Check the docs for more pieces of information:查看文档以获取更多信息:

Think of setState() as a request rather than an immediate command to update the component.将 setState() 视为更新组件的请求而不是立即命令。

The second parameter to setState() is an optional callback function that will be executed once setState is completed and the component is re-rendered. setState() 的第二个参数是一个可选的回调函数,一旦 setState 完成并重新渲染组件,就会执行该回调函数。 Generally we recommend using componentDidUpdate() for such logic instead.通常我们建议使用 componentDidUpdate() 来代替这种逻辑。

https://reactjs.org/docs/react-component.html#setstate https://reactjs.org/docs/react-component.html#setstate

The second argument is a callback that invoked after the state object is changed(since setState is async).第二个参数是在状态对象更改后调用的回调(因为setState是异步的)。

this.setState({someState...}, () => console.log('someState is changed'))

If you need to preform some action after the state is changed It's better if you you use componentDidUpdate or useEffect hook in React ^16.7如果你需要在 state 改变后执行一些 action 最好在 React ^16.7使用componentDidUpdateuseEffect hook

React's setState method takes an optional second argument, which is a callback: React 的setState方法接受一个可选的第二个参数,它是一个回调:

The second parameter to setState() is an optional callback function that will be executed once setState is completed and the component is re-rendered. setState()的第二个参数是一个可选的回调函数,一旦setState完成并重新渲染组件,就会执行该回调函数。 Generally we recommend using componentDidUpdate() for such logic instead.通常我们建议使用componentDidUpdate()来代替这种逻辑。 (also see the docs ). (另见文档)。

setState works asynchronously. setState异步工作。 React will accumulate state change requests, and execute them when it can. React 会累积状态更改请求,并在可能的时候执行它们。 There, when you call setState , you won't know when your state actually changes.在那里,当您调用setState ,您将不知道您的状态何时实际更改。 For this reason, you can provide setState with a callback which is executed when the changes are complete.因此,您可以为setState提供一个回调,该回调在更改完成时执行。

Or better yet, use componentDidUpdate , which is called by React whenever the state changes.或者更好的是,使用componentDidUpdate ,每当状态发生变化时,React 都会调用它。

the setState second argumend is a callback. setState 第二个参数是回调。 That means when you use handleChange function, the function filterItems will run afer set state of the input.这意味着当您使用 handleChange 函数时,函数 filterItems 将在输入的设置状态之后运行。

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

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