【问题标题】:Redux Middleware executing action without next calledRedux 中间件执行操作而不调用 next
【发布时间】:2019-06-05 05:51:38
【问题描述】:

我有一个使用 accessToken + refreshToken 来使用 API 的应用程序。 我想添加一个中间件来检查 accessToken 并在运行 API 调用之前根据需要刷新它。

我面临的问题是,即使我不执行下一个方法,我的初始操作也总是被调度。

这是我的中间件:

const requestMiddleware = store => next => action => {
  const {user, accessToken} = loadStorage();
  const payload = jwtDecoder(accessToken.access_token);
  const refreshThreshold = (Date.now() + 1000) / 1000;
  if(accessToken.refresh_token && refreshThreshold > payload.exp){
    // Here we defined that we need to refresh the token
    userConnexion.refresh(user.uuid, accessToken.refresh_token)
        .then((accessToken) => {
            // Token updated we can now run the next action
            return next(action);
          }, (error) => {
              // Error on token refresh we throw an error
              return Promise.reject(error.response.data.message);
          });
}else{
    // AccessToken is still valid so we continue with the next action
    return next(action);
}

}

例如,如果我调度了一个动作 USER_UPDATE,即使我没有运行发送任何下一个动作,它也会调用该动作。

我想我错过了一些关于 redux 中间件的东西,但我不知道它是什么。

我正在使用 redux-think 来处理我的异步操作,我认为问题在于我使用它的方式,但我不知道如何处理它。 我是否必须阻止下一个操作自动运行?

感谢您的帮助。

【问题讨论】:

    标签: reactjs redux redux-thunk


    【解决方案1】:

    我终于找到了解决问题的方法。 看来我需要在 thunk 中间件之前应用我的中间件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-23
      • 1970-01-01
      • 2018-06-12
      • 2017-06-23
      • 2018-05-13
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多