【发布时间】:2017-04-05 23:44:59
【问题描述】:
我有一个可以工作的 Redux 减速器,除了一个未设置的布尔变量。代码:
const GameBoardReducer = function(state, action) {
if( state === undefined ) {
let state = {
rows: BOARD_ROWS,
cols: BOARD_COLS,
width: Math.min( document.body.clientWidth * 0.9, 1184 ),
board: [],
running: false,
framesPerSec: 5,
}
for( var i=0 ; i<BOARD_ROWS ; i++ ) {
state.board.push([]);
for( var j=0 ; j<BOARD_COLS ; j++ ) {
state.board[i].push(Math.random()<0.15? FIELD_ALIVE: FIELD_DEAD);
}
}
return state;
}
switch(action.type) {
case SET_BOARD_ACTION:
return { ...state, board: action.payload };
case RUN_ACTION:
console.log("RunAction:", action.payload);
return { ...state, running: action.payload };
}
return state;
}
控制台显示
RunAction: 真
但容器中的值:this.props.gameBoard.running 仍然是 false。
为什么?
【问题讨论】:
-
人生游戏正在运行和停止?