简体   繁体   English

如何在 React 中使用 useState 挂钩更新 state

[英]How does updating state with useState hook in React work

I understand how updating state using the useState hook in React works as follows.我了解使用 React 中的useState挂钩更新 state 的工作原理如下。

  1. Compare the state value to be changed with the current state value.将要更改的 state 值与当前 state 值进行比较。
  2. If the two state values are the same, do not re-render without changing the state value.如果两个 state 值相同,则不要在不更改 state 值的情况下重新渲染。
  3. If the two state values are different, change the state value and perform re-rendering.如果两个 state 值不同,则更改 state 值并执行重新渲染。

https://codesandbox.io/s/nostalgic-fog-riyg4 https://codesandbox.io/s/nostalgic-fog-riyg4

But in this example, it seems to work differently.但在这个例子中,它的工作方式似乎有所不同。

  • First rendering execute when App component is mounted.安装 App 组件时执行第一次渲染。
  • State value changes when button is clicked and a second rendering is executed.单击按钮并执行第二次渲染时,State 值会发生变化。 (Because the current state value and the state value to be changed are different.) (因为当前 state 值和要更改的 state 值不同。)
  • When you click the button again, the state value does not change but rendering occurs.当您再次单击该按钮时,state 值不会更改,但会进行渲染。 -> Why is this happening? -> 为什么会这样? I didn't expect rendering because the two state values were the same.我没想到会渲染,因为两个 state 值相同。
  • When you click the button a third time, the state value does not change again, but this time the component does not re-render.当您第三次单击该按钮时,state 值不会再次更改,但这次组件不会重新渲染。 This is the behavior I expected to happen after the second click.这是我期望在第二次点击后发生的行为。

A React component will update every time its state or props change.每当其stateprops更改时,React 组件都会更新。 These two artifacts of a react component, can't be changed in render function or any functions called by render . react 组件的这两个工件,不能在render function 或render调用的任何函数中更改。

Also, you have to remember that you can't ever change the props of a component within the component itself.此外,您必须记住,您永远无法在组件本身内更改组件的 props。 the props are passed from parent and cant be changed by the child.道具是从父母传递的,孩子不能改变。 But for the state , you can call setState in onClick functions or any other function outside the render , and any time you call the setState , your component will update its view.但是对于state ,您可以在onClick函数或任何其他render之外调用setState ,并且任何时候调用它的setState ,您的组件视图。

After getting familiar with ReactJs, I suggest you use Redux for any kind of data manipulation and functionality.在熟悉 ReactJs 之后,我建议您使用 Redux 进行任何类型的数据操作和功能。 Here is a link to react-redux website: https://react-redux.js.org/这是 react-redux 网站的链接: https://react-redux.js.org/

I hope this answers your question我希望这回答了你的问题

for updating the general usestate function: const [initialState, setState] = useState(initialState)用于更新一般使用状态 function: const [initialState, setState] = useState(initialState)

initialState is the initial state of the component and initialState是组件的初始 state 和

setState is the function that updates the initialState setState是更新初始状态的 function

for more information about useState Hooks in react: Link: https://blog.bitsrc.io/understanding-react-hooks-usestate-6627120614ab有关 react 中的 useState Hooks 的更多信息:链接: https://blog.bitsrc.io/understanding-react-hooks-usestate-6627120614ab

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

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