javascript - 当 API 响应时调用 componentDidMount

标签 javascript reactjs redux

在我的项目中,我调用了一个操作,该操作进行 Web 服务调用,然后将操作分派(dispatch)到 ws 的结果,这些操作编辑商店。 我的问题在于:

ComponentDidUpdate () {
    If (this.props.messages.length) {
      Const items = this.props.messages.filter (this.isDisplayable);
      This.timer = setInterval (() => {
        If (items.length> 0) {
          This.props.popItem (items);
        } Else {
          ClearInterval (this.timer);
        }
      }, This.props.interval);
    }
  }

事实上,它已经启动了好几次,并且我收到了警告

Warning: flattenChildren (...): Encountered two children with the same key, 1. Child keys must be unique; When two children share a key, only the first child will be used.

我使用了componentDidMount,但它在 API 响应之前启动它。

我的问题是: 有没有办法仅在响应我的操作时更新组件,或者传递警告?

最佳答案

试试这个:

componentWillReceiveProps(nextProps) {
    if (this.props.messages === nextProps.messages) return;

关于javascript - 当 API 响应时调用 componentDidMount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41981077/

相关文章:

javascript - ReactJS Promise resolve() 不会将值传递给 .then() ,而是reject() 将值传递给 .catch()

javascript - 如何一次播放数组中的一首歌曲 react js

javascript - 在 Redux 状态下存储具有原型(prototype)函数的对象

javascript - react redux 异步 mapStateToProps

javascript - 获取元素 ( ID ) 并将其显示在页面的任何位置

javascript - Underscore的查找数组功能如何实现

javascript - 使用 Rails 上传文件?

javascript - 如何创建与其他对象类型相同的新对象

javascript - React 组件渲染不正确

javascript - 如何在 Redux 中更新状态后触发关闭回调?