我的React代码中有一个函数定义如下:
getAttachment(url) { fetch(url).then((responseText) => { var response = responseText.json(); response.then(function(response){ this.setState({ attachment: response }); }); }.bind(this)); }
但是我在编译时收到一个错误,说我在处有一个意外的标记.bind(this)。有什么想法,如何在诺言中设定状态?
.bind(this)
除了绑定外,this您还可以将引用范围限定在this。喜欢
this
var that = this;
然后参考that.setState。
that.setState