2017-07-15 89 views
0

我在渲染父級時遇到了React JS渲染子級的一些問題。 這裏我試圖實現「遊戲人生」(Freecodecamp類的一個項目)。 我被困在這種情況下。我點擊一個死細胞,它變得活着(藍色)。然後,假設我想清除網格,即使所有單元格都死掉,但它不起作用。看起來,即使重新渲染父母也不會重新渲染孩子。React JS渲染父級不再渲染子級

有什麼想法?

var board = []; 
 
var width = 80; 
 
var length = 50; 
 
var cells = width * length; 
 
var states = ["alive", "dead"]; 
 

 

 

 
class BoardGrid extends React.Component { 
 
    constructor(props) { 
 
    super(props); 
 
    //this.initBoardArray = this.initBoardArray.bind(this); 
 
    
 
    } 
 

 
    render() { 
 
    //this.initBoardArray(); 
 
    
 
    let boardDOM = this.props.board.map(function(cell) { 
 
     return <BoardGridCell status={cell.status} id={cell.id} />; 
 
    }); 
 

 
    return (
 
     <div id="game-grid"> 
 
     {boardDOM} 
 
     </div> 
 
    ); 
 
    } 
 
} 
 

 
class BoardGridCell extends React.Component { 
 
    render() { 
 
    return (
 
     <div 
 
     id={this.props.id} 
 
     className={`cell ${this.props.status}`} 
 
     data-status={this.props.status} 
 
     /> 
 
    ); 
 
    } 
 
} 
 

 
function initBoard() { 
 
    for (let cellIndex = 0; cellIndex < cells; cellIndex++) { 
 
     let cell = { id: cellIndex, status: "dead" }; 
 
     board[cellIndex] = cell; 
 
    } 
 
} 
 

 
function drawBoard() { 
 
    ReactDOM.render(
 
    <BoardGrid board={board} />, 
 
    document.getElementById("game-grid-wrapper") 
 
); 
 
} 
 

 
function clearBoard() { 
 
    for (let cellIndex = 0; cellIndex < cells; cellIndex++) { 
 
     let cell = { id: cellIndex, status: "dead" }; 
 
     board[cellIndex] = cell; 
 
    } 
 
} 
 

 
$("#game-grid-wrapper").on("click", ".cell", function() { 
 
    let currentState = $(this).attr("data-status"); 
 
    let currentStateIndex = states.indexOf(currentState); 
 
    let newState = states[(currentStateIndex + 1) % 2]; 
 
    $(this).attr("class", `cell ${newState}`); 
 
    $(this).attr("data-status", newState); 
 
}); 
 

 
$("#stop").on("click", function() { 
 
    alert("clearing"); 
 
    clearBoard(); 
 
    drawBoard(); 
 
}); 
 

 

 
initBoard(); 
 
drawBoard();
html, 
 
body { 
 
    height: 100%; 
 
    text-align: center; 
 
    font-family: 'Open Sans', sans-serif; 
 
} 
 

 
h1, 
 
h2 { 
 
    font-family: 'Press Start 2P', cursive; 
 
} 
 

 
.button { 
 
    width: 100px; 
 
    border: 1px solid #555; 
 
    padding: 5px; 
 
    margin: 5px; 
 
    cursor: pointer; 
 
    border-radius: 4px; 
 
} 
 

 
.button:hover { 
 
    opacity: 0.9; 
 
} 
 

 
#main { 
 
    margin: 10px; 
 
} 
 

 
#game-grid { 
 
    background-color: #000; 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    align-content: flex-start; 
 
    width: 800px; 
 
    height: 500px; 
 
    overflow: hidden; 
 
} 
 

 
#game-grid .cell { 
 
    border: 1px solid #767676; 
 
    width: 10px; 
 
    height: 10px; 
 
    color: #fff; 
 
    font-size: 9px; 
 
    box-sizing: border-box; 
 
} 
 

 
.alive { 
 
    background-color: #2185d0; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="main"> 
 
<div id="game-actions"> 
 
     <div id="start" class="button"><i class="fa fa-play"></i> Start</div> 
 
     <div id="pause" class="button"><i class="fa fa-pause"></i> Pause</div> 
 
     <div id="stop" class="button"><i class="fa fa-stop"></i> Stop</div> 
 
     </div> 
 
    <div id='game-grid-wrapper'></div> 
 
</div>

回答

0

你不應該使用jQuery與一起反應,如果你沒有到。都操縱DOM,但基於不同的信息,這可能會使他們以意想不到的方式干擾。

對於您的董事會州,您應該use the state of your BoardGrid component。在渲染時,在構造函數中初始化您的狀態,並在每個單元格中初始化您的狀態add an onClick() callback

單擊單元格時,調用板組件給出的回調函數,並將其傳遞給它。使用該ID在BoardGrid組件中使用setState()來更新電路板狀態。

如果您遇到任何問題,我可以稍後添加一些示例代碼。