我正在从teamtreehouse.com制作一个非常基本的React应用程序,而且我经常遇到
"TypeError:无法读取未定义的属性'onPlayerScoreChange'"
即使我正确绑定我的功能(我认为)
'onPlayerScoreChange'Grandparent组件中的一种方法,当用户点击"+"或" - "按钮更改玩家的分数时执行该方法.
如果有人可以解释错误,那将是非常有帮助的,因为我认为我正在设置this.onPlayerScoreChange = this.onPlayerScoreChange.bind(this)伟大的祖父母的构造函数.
父组件:
class App extends React.Component {
constructor(props) {
super(props);
this.onPlayerScoreChange = this.onPlayerScoreChange.bind(this)
this.state = {
initialPlayers: props.initialPlayers,
};
}
onPlayerScoreChange(delta, index) {
this.setState((prevState, props) => {
return {initialPlayers: this.prevState.initialPlayers[index].score += delta}
})
}
render() {
return(
<div className = "scoreboard">
<Header title = {this.props.title}/>
<div className = "players">
{this.state.initialPlayers.map(function(player, index) {
return(
<Player
name = {player.name}
score = {player.score}
key = {player.id}
index = {index}
onScoreChange = …Run Code Online (Sandbox Code Playgroud)