您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在React中Promise中的setState

在React中Promise中的setState

除了绑定外,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

其他 2022/1/1 18:14:22 有680人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶