【问题标题】:Wait for Completion in reactjs [duplicate]在reactjs中等待完成[重复]
【发布时间】:2018-07-25 20:49:58
【问题描述】:

在我的反应组件中,我从 http get 将数据加载到表中。

当我删除一条记录时,我在后端执行删除并在反应中再次调用 fetch tabledata。有时,已删除的行不会从 GUI 中的表中删除。我认为需要等到执行删除,以便只有在我知道该行被删除时才获取表数据。

我的删除功能:

handleBtnClick_RecordDeletion(e) {
    this.httpDeleteJson(this.state.ip + /delete?id=" + e.name);
    alert("Row deleted Successfully");
    this.setState({ lastDeletedRow: e.name })
    this.httpGetAll(this.state.moduleName, this);
}

【问题讨论】:

    标签: reactjs ecmascript-6


    【解决方案1】:

    尝试在setState 之后使用回调,如下所示:

    this.setState({ lastDeletedRow: e.name }, ()=>{
         this.httpGetAll(this.state.moduleName, this);
    });
    

    解释:

    setState() 不会立即改变 this.state 而是创建一个 等待状态转换。调用 this 后访问 this.state 方法可能会返回现有值。没有 保证对 setState 的调用和调用的同步操作可能 进行批处理以获得性能提升。

    来源:https://vasanthk.gitbooks.io/react-bits/patterns/19.async-nature-of-setState.html

    附: 另外,在重复标记中有有用的参考

    【讨论】:

    • "..可能会返回 现有值" — 我认为我们不需要更新值 this.state.lastDeletedRow 来发出该请求
    • 我认为问题仅在于“this.httpDeleteJson()”和“this.httpGetAll(): the deletion command can wait for batabase table lock or something, so the "get all" request get some data before the deletion actually occurs. The solution is, for example, to return a Promise from this.httpDeleteJson() and call this.httpGetAll in .then()”之间的并发性`
    猜你喜欢
    • 1970-01-01
    • 2016-04-08
    • 2015-11-19
    • 2017-12-17
    • 2015-04-01
    • 2013-04-03
    • 2019-02-01
    • 2020-01-12
    • 1970-01-01
    相关资源
    最近更新 更多