Reactjs Redux减速机赢得';t集布尔对象属性
我有一个Redux reducer可以工作,除了一个未设置的布尔变量。守则:Reactjs Redux减速机赢得';t集布尔对象属性,reactjs,redux,reducers,Reactjs,Redux,Reducers,我有一个Redux reducer可以工作,除了一个未设置的布尔变量。守则: 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
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;
}
const GameBoardReducer=函数(状态、操作){
如果(状态===未定义){
让state={
行:板_行,
科尔斯:董事会,
宽度:Math.min(document.body.clientWidth*0.91184),
董事会:[],
跑步:错,
framesPerSec:5,
}
对于笔中的(var i=0;i,您正在setInterval
中保存对gameboard的引用,并检查它是否正在运行,而不是在此.props中检查gameboard。请尝试以下更改:
this.state = {
interval: setInterval(() => this.calcNextGeneration(), ms)
};
calcNextGeneration() {
console.log("calcNextGeneration", this.props.gameBoard.running);
if( !this.props.gameBoard.running ) return;
...
而生命的游戏是运行和停止,因为它应该?