【发布时间】:2019-01-01 17:51:31
【问题描述】:
每个人 我是 React Native 的新手,正在尝试一些可能超出我想象的东西。我希望使用 fetch 命令来提取数据并将其保存为我的状态的属性。这是我的代码:
componentDidMount() {
fetch('http://swapi.co/api/people/')
.then(response => response.json())
.then(responseData => {
this.setState({
tweets: responseData.results
});
console.log(this.state.tweets)
})
.catch(error => {
alert('Fetching and parsing data error ' + error);
})
.then(console.log(this.state.tweets));
} 第一个 console.log() 输出我要拉的正确数组,但第二个 console.log() 显示 null。
有人可以解释为什么会这样吗? 谢谢
【问题讨论】:
-
您也可以在
console.log中添加一些字符串以查看首先调用的内容。前任。console.log('after setting state', this.state.tweets)
标签: javascript api react-native fetch