React hook:使用参数或箭头函数设置状态之间的区别

React hook: difference between setting the state with an argument or an arrow function

我想在下一期中详细介绍状态更新

这两种更新{count}的方式有区别吗? 它们严格等效吗?

<button onClick={()=>setCount**(count+1)**} >{count}</button>

<button onClick={()=>setCount(**savedCount => savedCount+1)**}{count}</button>

提前致谢

不,他们不是。您应该更喜欢最新的,因为它保证您正在使用的 count 是最新版本。当直接使用 count 而不是 savedCount 时,它 可能 有一些更新仍未应用。

不,两者没有区别。

第一个通过作用域的概念有了count的值

第二个是使用回调从 useState 挂钩获取值,它始终提供最新值。