简体   繁体   English

无法理解 React setState 签名“setState(updater[, callback])”

[英]Cannot understand React setState signature "setState(updater[, callback])"

I know this is probably a JavaScript question rather than a React question, but I cannot understand the above signature of React setState.我知道这可能是一个 JavaScript 问题而不是一个 React 问题,但我无法理解上面的 React setState 签名。

What do the square brackets and the comma do inside the function parameter list?函数参数列表中的方括号和逗号有什么作用?

I know how to use it with an updater, but how to use it with callback and again what is this syntax?我知道如何将它与更新程序一起使用,但是如何将它与回调一起使用,这又是什么语法?

If you only want to update the state, then you can use setState({ key: 'value' })如果你只想更新状态,那么你可以使用setState({ key: 'value' })

And if you want to perform an action on the updated state just after updation, then you can run setState({ key: 'value' }, () => callback()) or setState({ key: 'value' }, callback)如果您想在更新后立即对更新的状态执行操作,那么您可以运行setState({ key: 'value' }, () => callback())setState({ key: 'value' }, callback)

The callback function should be callback() { } or callback = () => { } in react class component React 类组件中的回调函数应该是callback() { }callback = () => { }

The callback function will run after updating the state.回调函数将在更新状态后运行。

Note: We use callback because setState use some microseconds to update the state and the next line will be run before updating the state.注意:我们使用回调是因为 setState 使用一些微秒来更新状态,并且下一行将在更新状态之前运行。 Then we pass callback which runs after updating the state.然后我们传递在更新状态后运行的回调。

What do the square brackets and the comma do inside the function parameter list?函数参数列表中的方括号和逗号有什么作用?

That just indicates that passing in the second argument is optional.这只是表明传入第二个参数是可选的。 (This is a very common notation in JavaScript and elsewhere.) You can call setState like this: (这是 JavaScript 和其他地方非常常见的表示法。)您可以像这样调用setState

this.setState(newStateOrUpdaterFunction);

or like this:或者像这样:

this.setState(newStateOrUpdaterFunction, callbackFunction);

In the second case, React will call callbackFunction when the state update is done.在第二种情况下,当状态更新完成时,React 将调用callbackFunction This is useful sometimes because state updates are asynchronous .这有时很有用,因为状态更新是异步的

It's relatively rare to want to pass in that second argument.想要传入第二个参数的情况相对较少。 Normally, you just want to re-render after updating state, which React will do automatically by calling your render function.通常,您只想在更新状态后重新渲染,React 会通过调用您的render函数自动执行此操作。

It's an optional parameter.它是一个可选参数。

You may use it the following way :您可以通过以下方式使用它:

setState( updater, () => { AfterAllSetstateDone_doSomeActionHere; } );

The callback is used to inform when all setState calls have been treated.回调用于通知何时处理了所有setState调用。

But it's optional since you'd rather use the componentDidUpdate() { someActionHere } lifecycle hook in order to deals with fullfilled setState calls.但它是可选的,因为您宁愿使用componentDidUpdate() { someActionHere }生命周期钩子来处理完整的setState调用。

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

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