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