【发布时间】:2017-04-18 23:09:19
【问题描述】:
我正在尝试从 GET 请求(这是一个对象数组)中获取响应,并将其状态设置为数组“模板”,以便以后可以使用 this.state.templates 访问它,就像这样:
fetch('https://tiy-warmup.herokuapp.com/api/templates?token=' +
token)
.then(response => response.json())
.then(response => this.setState({templates: response}))
// .then(response => console.log(response))
当我将 setState 行注释掉并在控制台记录响应时,我得到了正确的数组,所以我知道我的 fetch 是好的。但是,当我尝试运行 setState 行时,出现此错误:
Possible Unhandled Promise Rejection (id: 0):
_this2.setState is not a function
解决此错误并能够在渲染中使用 this.state.templates 的解决方案是什么?
【问题讨论】:
标签: reactjs react-native setstate