我有嵌套App->GameList->GameItem
陣營兒童調用父方法無法訪問狀態
我有通過onClick
事件在GameItem(child)
組件
當得到調用App(parent)
組件上的方法3個組件GameItem
組件被點擊它觸發App
組件方法來訪問App
組件中的狀態。
然而,當我嘗試訪問狀態this.state.gamesVM
我得到一個
Uncaught TypeError: Cannot read property 'gamesVM' of null
。
APP COMPONENT
export default class App extends React.Component {
state = {
userId: 3,
gamesVM: [],
}
componentDidMount() {
const pGames = getGames();
const pPicks = getPicks();
const pTeams = getTeams();
Promise.all([pGames, pPicks, pTeams])
.then(payload => {
const gamesVM = this.getGamesVM(payload);
this.setState({gamesVM});
});
}
getGamesVM(payload) {
// ... code to get and map gamesVM
}
teamPicked(team, away, home) { // gets called from child component
console.log(this.state.gamesVM); // Uncaught TypeError: Cannot read property 'gamesVM' of null
console.log(this.state.userId); // Uncaught TypeError: Cannot read property 'userId' of null
// ...
console.log(this.state.anything); // Uncaught TypeError: Cannot read property 'anything' of null
}
render() {
return (
<div className="App">
<form onSubmit={this.handleSubmit}>
<GameList
gamesVM={this.state.gamesVM}
teamPicked={this.teamPicked}
/>
<input type="submit" value="Save" />
</form>
</div>
);
}
}
。
遊戲列表COMPONENT
export default class GameList extends React.Component {
render() {
return (
<div className='matchups'>
{this.props.gamesVM.map(game =>
<GameItem
teamPicked={this.props.teamPicked}
key={game.id}
{...game}
/>
)}
</div>
)
}
}
。
GAMEITEM COMPONENT
export default class GameItem extends React.Component {
render() {
let awayPickCheckbox = null;
if (!this.props.isGameStarted) {
awayPickCheckbox = <li>
<input type="checkbox" id={this.props.id + this.props.awayTeam} />
<label
htmlFor={this.props.id + this.props.awayTeam}
onClick={this.props.teamPicked.bind(this, 'away', this.props.id + this.props.awayTeam, this.props.id + this.props.homeTeam)}
>
{this.props.awayTeam}
</label>
</li>;
} else {
awayPickCheckbox = <li>
<label>{this.props.awayTeam}</label>
</li>
}
return (
<div className="single-matchup">
<ul className="away-team clearfix">
{awayPickCheckbox}
</ul>
</div>
)
}
}
這個答案適合我 – locnguyen