0

——首先为这个模棱两可的标题道歉。我遇到的问题我真的很困惑如何放置标题。我愿意编辑。基本上,我使用的是 react-redux,并且我有一个模式,我正在更新数据库中的一些内容。更新时,我希望所有减速器状态默认并关闭模式。(这是直截了当的,因为我在更新时触发了一个将所有值设置为默认值的函数)但是,在更新时,我想关闭模式并在更新时显示一个 toastr 或来自响应的错误消息。我已经为 toastr 设置了组件和操作。

问题是 toastr 不显示消息,因为在它可以匹配来自 store 的常量操作之前,模态的 reducer 已经设置为默认值。克服这个问题的最佳方法是什么?

模态的.jsx

//update 
   updateConfig() {
            this.props.updateConfigNode(node, latestConfigData); //update action
            this.closeModal(); 
        }
    closeModal() {
        this.props.status(false); //Close modal
        this.props.update(''); //Set update action status 'updated or error' to ''
      }

Toastr.jsx

    componentDidUpdate() {
     this.routerConfigNotification(this.props.config); //Getting from store
   }
    configNotify(config) {
        const message = checkForRouterConfigUpdateState(config);
    if(message) {
            this._addNotificationNormal(message.title, message.type, message.text);
          }
    }

实用程序

    export function checkForRouterConfigUpdateState(routerConfig) {

  let message = {};
  let messageText;

  switch (_.get(routerConfig, 'updateStatus')) {
case '_error':
      messageText = 'An error has been occurred while updating configuration';
      return message = {
        mode: 'normal',
        title: 'Error',
        type: 'info',
        status: _.get(routerConfig, 'updateStatus'),
        text: messageText
      };

    case '_updated':
      messageText = 'Successfully Updated';
      return message = {
        mode: 'normal',
        title: 'Updated',
        type: 'info',
        status: _.get(routerConfig, 'updateStatus'),
        text: messageText
      };
}
}

我尝试过其他生命周期更新方法,但最接近解决此问题的是使用 setTimeout 函数,我知道这是一个 hack,我很确定有一种方法可以解决这些异步问题。先感谢您。

4

1 回答 1

0

this.props.updateConfigNode(node, latestConfigData)承诺吗?如果是这样,您可以放入closeModal一个then块中,以便仅在您的请求完成时调用它。

this.props.updateConfigNode(node, latestConfigData).then(() => {
  this.closeModal();
});
于 2016-07-23T04:33:41.743 回答