gpt4 book ai didi

reactjs - 我应该将 setState 包含在 useCallback 的依赖项数组中吗?

转载 作者:行者123 更新时间:2023-12-03 13:25:58 24 4
gpt4 key购买 nike

    const [active, setActive] = useState(false);

const onActiveChanged = useCallback(
isActive => () => {
// do something
setActive(isActive);
},
[setActive], // or just [] is okay?
);

当同时使用 useStateuseCallback (或 useMemo)时,我应该在数组中包含 setState依赖关系?

最佳答案

对此的建议也在React Docs - Hooks API Reference上.

The setState function is used to update the state. It accepts a new state value and enqueues a re-render of the component.

setState(newState);

During subsequent re-renders, the first value returned by useState will always be the most recent state after applying updates.

Note

React guarantees that setState function identity is stable and won’t change on re-renders. This is why it’s safe to omit from the useEffect or useCallback dependency list.

关于reactjs - 我应该将 setState 包含在 useCallback 的依赖项数组中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55738134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com