【发布时间】:2021-11-11 12:07:25
【问题描述】:
我在响应中调用我的 api 完全没有得到任何响应:
constructor(props) {
super(props);
this.state = {
team: {}
}
}
getData() {
axios.get('http://game.test/api/characters')
.then(response => this.setState({ team: response.data.characters }));
}
componentDidMount() {
this.getData();
console.log(this.state);
}
状态是空的,如果我 console.log 在 .then 中的任何内容,控制台也是空的(就像代码的那部分不可访问)。 同样在“网络”选项卡上,一切似乎都很好(状态 200 和正确的数据)。
【问题讨论】:
-
async.. await? -
这能回答你的问题吗? Use Async/Await with Axios in React.js
-
我们真的帮不上什么忙:你还没有向我们展示你是如何使用
console.log的,我们也看不到 JSON 的结构是什么。 -
@YevgenGorbunkov — 在我看来,
this和getData的使用很好。
标签: javascript reactjs