简体   繁体   English

尝试在单击按钮时切换 Redux 布尔值,但是切换仅在第一次起作用,我不确定为什么

[英]Trying to toggle a Redux boolean on a button click, however the toggle only works the first time and I'm not sure why

I have a very simple redux slice that has 2 reducers which toggle a boolean to either on or off and a function inside of a hook that dispatches these actions depending on the current state我有一个非常简单的 redux slice,它有 2 个 reducer,可以将布尔值切换为打开或关闭,以及一个挂钩内的函数,该函数根据当前状态分派这些操作

Try尝试

const toggleForceNativeActivities = useCallback(() => {
    forceNativeActivities ? dispatch(toggleForceNativeActivitiesOff()) : dispatch(toggleForceNativeActivitiesOn());
    setItem(PERSISTED_FORCED_ACTIVITY_KEY, forceNativeActivities.toString());
}, [forceNativeActivities]);

Your handler is working with a stale value without useCallback .您的处理程序正在使用没有useCallback的陈旧值。

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

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