class App extends Component {
state = {
persons: ''
}
componentDidMount() {
axios.get(`https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=pokemon&utf8=&format=json`)
.then(res => {
const persons = res;
this.setState({
persons
});
console.log(this.state.persons.data.query.search[0].title);
})
}
render() {
return (
<div className="App">
<p>this.state.persons.data.query.search[0].title</p>
</div>
);
}
}
答案 0 :(得分:1)
这是因为您的state.persons
在初始化时为空,请尝试首先进行验证:
render() {
return (
<div className="App">
<p>{this.state.persons ? this.state.persons.data.query.search[0].title : null}</p>
</div>
);
}