[英]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 的工作原理如下。
https://codesandbox.io/s/nostalgic-fog-riyg4 https://codesandbox.io/s/nostalgic-fog-riyg4
But in this example, it seems to work differently.但在这个例子中,它的工作方式似乎有所不同。
A React component will update every time its state
or props
change.每当其
state
或props
更改时,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.