【发布时间】:2019-06-18 14:48:08
【问题描述】:
我们知道,如果我们在 Function Component 中使用 useState,则不会每次都在该 Function 组件的 re-renders 上创建状态,而是使用现有状态。请参阅下面的 Example 函数组件:
function Example() {
// Declare a new state variable, which we'll call "count"
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
当我们在customHook 中使用useState 时(见下文),每次调用“useCustomHook”都会创建一个新状态,这表明所有自定义挂钩都只是常规函数。
function useCustomHook() {
const [cnt, setCnt] = useState(0);
return [cnt, setCnt];
}
【问题讨论】:
标签: javascript reactjs state react-hooks