onScoreChange = {this.onPlayerScoreChange},
您可以使用绑定或箭头功能进行绑定
需要PS绑定,因为map函数的上下文不同于React Component上下文,因此
this该函数内部不会引用React Components
this,因此您无法访问React Component类的该属性。
具有箭头功能:
{this.state.initialPlayers.map((player, index)=> { return( <Player name = {player.name} score = {player.score} key = {player.id} index = {index} onScoreChange = {this.onPlayerScoreChange} /> ) })}
带绑定
{this.state.initialPlayers.map(function(player, index) { return( <Player name = {player.name} score = {player.score} key = {player.id} index = {index} onScoreChange = {this.onPlayerScoreChange} /> ) }.bind(this))}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)