您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

您尚未绑定要使用的map函数onscoreChange = {this.onPlayerscoreChange}

您可以使用绑定或箭头功能进行绑定

需要PS绑定,因为map函数的上下文不同于React Component上下文,因此this函数内部不会引用React Componentsthis,因此您无法访问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))}
其他 2022/1/1 18:18:15 有463人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶