【发布时间】:2021-03-24 13:45:48
【问题描述】:
我的问题是当我试图在我的 useReducer 函数中切换布尔值时,这样做会导致值变回原始值的问题:
function reducerBalls(state: any, action: any) {
let newState;
let item;
switch (action.type) {
case ACTIONS.INIT:
return action.balls;
case ACTIONS.SELECTED:
newState = [...state];
item = newState[action.index] ;
item.active = !item.active;
return newState;
default:
return state;
}}
这里是调度事件
function ballCheckboxHandler(ball: lotteryBalls, event: any) {
if(event.target.checked) {
return dispatch({type: ACTIONS.SELECTED, index: ball.number});
}
if(event.target.checked === false) {
return dispatch({type: ACTIONS.UNSELECTED, index: ball.number});
}
}
现在我知道 react.StrictMode 是导致此问题的原因,并且他们说实时模式不会发生此问题,但问题归结为它的开发。
【问题讨论】:
-
尝试将
event.target.checked === true添加到第一个条件
标签: reactjs react-hooks use-reducer