简体   繁体   English

React JS渲染父级不会再次渲染子级

[英]React JS Rendering parent does not render children again

I am having some issues with React JS rendering children when rendering the parent. 我在渲染父代时有一些与React JS渲染子代有关的问题。 Here I am trying to implement the "Game of Life" (a project from Freecodecamp class). 在这里,我正在尝试实现“生命游戏”(Freecodecamp类的一个项目)。 I am stuck in this situation. 我陷入这种情况。 I click on a dead cell and it becomes alive (blue). 我单击一个失效的单元,它变为活动状态(蓝色)。 Then, suppose I want to clear the grid, that is, make all cells dead, but it doesn't work. 然后,假设我要清除网格,也就是说,使所有单元格都失效,但是它不起作用。 It seems that even re-rendering the parent will not re-render the children. 似乎即使重新渲染父母也不会重新渲染孩子。

Any idea? 任何想法?

 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> 

You should not use jQuery together with React if you don't have to. 不必将jQuery与React一起使用。 Both manipulate the DOM but based on different information which can make them interfere in an unexpected way. 两者都操纵DOM,但是基于不同的信息,这可能使它们以意想不到的方式进行干扰。

For your board state you should use the state of your BoardGrid component . 对于您的板状态,应使用BoardGrid组件的状态 Initialize your state in the constructor and add an onClick() callback to each cell when rendering it. 在构造函数中初始化您的状态,并在呈现每个单元格时向其添加onClick()回调

When the cell is clicked call the callback function given by the board component and pass it's id with it. 单击该单元格时,调用板组件提供的回调函数,并将其ID传递给它。 Use that id to update the board state using setState() in your BoardGrid component. 使用该ID在BoardGrid组件中使用setState()更新板状态。

I can add some example code later, if you struggle with anything. 如果您遇到任何麻烦,我以后可以添加一些示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM