2017-04-05 28 views
1

我有Redux reducer,除了未設置的布爾變量外,它可以工作。代碼: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), 
     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還是假的。

爲什麼?

完整筆:http://codepen.io/lafisrap/pen/yMZyXw

+0

而生活遊戲正在運行和停止,因爲它應該? – Michael

回答

1

在你的筆,你在setInterval控股的遊戲鍵盤的參考和檢查,看看它的運行,而不是檢查this.props的遊戲鍵盤。嘗試這些更改:

this.state = { 
     interval: setInterval(() => this.calcNextGeneration(), ms)  
    }; 

calcNextGeneration() { 
    console.log("calcNextGeneration", this.props.gameBoard.running); 
    if(!this.props.gameBoard.running) return; 
...