小编典典

在React中Promise中的setState

reactjs

我的React代码中有一个函数定义如下:

getAttachment(url) {
    fetch(url).then((responseText) => {

        var response = responseText.json();

        response.then(function(response){
            this.setState({ attachment: response });
        });
    }.bind(this));
}

但是我在编译时收到一个错误,说我在处有一个意外的标记.bind(this)。有什么想法,如何在诺言中设定状态?


阅读 568

收藏
2020-07-22

共1个答案

小编典典

除了绑定外,this您还可以将引用范围限定在this。喜欢

var that = this;

然后参考that.setState

2020-07-22