这是我的“层次结构”的样子:
Game
Table
TableRow
Cell
当单击一个单元格时,我想从Game中运行一个方法。React的设计与我以前的设计有所不同,我只是不能全神贯注地将Cell中的更改传达给Game。最好的方法是什么?
仅供参考,我正在尝试在React.js中创建Ultimate TTT。
您可以做的是将指向Game函数的指针作为属性传递给Child组件。例如:
var Game = React.createClass({
gameFunction: function() {
alert('gameFunction()');
},
render: function() {
return (
<div className="game">
<h1>Game</h1>
<Table gameFunction={this.gameFunction} />
</div>
);
}
});
var Table = React.createClass({
render: function() {
return (
<div className="table">
<table>
<tr>
<td onClick={this.props.gameFunction}>Cell</td>
</tr>
</table>
</div>
);
}
});
话虽这么说,我还是React的新手,不知道这是否是实现此目标的正确方法!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句