【问题标题】:How to asynchronously reset values redux如何异步重置值redux
【发布时间】:2016-11-25 10:37:23
【问题描述】:

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

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

Modal.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,我很确定有一种方法可以解决这类异步问题。 先感谢您。

【问题讨论】:

  • 嗯,拍得一塌糊涂,但为什么你坚持要在更新时重置模态状态?只需在它打开之前将其重新设置,你就可以了,不是吗?
  • 试过了,但看看在 toastr 之间是否有另一个动作触发器更新会显示模态,因为我没有将状态设置为默认值。明白我的意思了吗?
  • Thunk 用于 redux 中的异步操作。你调查过吗?
  • 是的,我也在使用它,但这并不能解决我的问题

标签: reactjs redux reactjs-flux redux-framework


【解决方案1】:

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

this.props.updateConfigNode(node, latestConfigData).then(() => {
  this.closeModal();
});

【讨论】:

  • 这不是一个承诺。
猜你喜欢
  • 2021-08-28
  • 2018-10-31
  • 1970-01-01
  • 2018-04-20
  • 2017-06-15
  • 1970-01-01
  • 2019-10-31
  • 1970-01-01
  • 2019-01-17
相关资源
最近更新 更多