我试图从一个GET请求(这是一个对象数组)中获取响应,并将其状态设置为一个数组“模板”,以便以后可以使用它进行访问。 state.templates,像这样:React Native this2.state错误
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行注释掉,只是控制台日志的反应,我得到正确的阵列回来了,所以我知道我取好。然而,当我尝试运行的setState线,我得到这个错误:
Possible Unhandled Promise Rejection (id: 0):
_this2.setState is not a function
什么将避过这一错误,并能够在渲染使用this.state.templates的解决方案?
谢谢!箭头功能工作。 –