我正在做一個很基本的應用程序,我經常會遇到“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 = {this.onPlayerScoreChange}
/>
)
})}
</div>
</div>
)}}(組件具有默認標題支持)兒童部分:class Player extends React.Component {render() {
return(
<div className = "player">
<div className = "player-name">
{this.props.name}
</div>
<div className = "player-score">
<Counter score = {this.props.score} onChange = {this.props.onScoreChange} index = {this.props.index}/>
</div>
</div>)}}
使用map時,Reaction‘無法讀取未定義的屬性’
素胚勾勒不出你
2019-07-13 16:07:53