使用地图时反应'无法读取未定义的属性'

使用地图时反应'无法读取未定义的属性',第1张

使用地图时反应'无法读取未定义的属性'

您尚未绑定要使用的map函数

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))}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5509279.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存