0

我有一个异步 actionCreator 来处理我的应用程序的身份验证流程:

function createAuthenticationResponse(err, grant) {
  return {
    type: AUTHENTICATION_RESPONSE,
    payload: err || grant,
    error: Boolean(err)
  }
}

function authenticate() {

  // return a thunk.
  return dispatch => {

    // Notify the system that we are authenticating.
    dispatch({ type: AUTHENTICATE });

    // Trigger the auth flow.
    myAuthModule.authorize((err, grant) => {

      // Trigger a state-change on the outcome.
      dispatch(createAuthenticationResponse(err, grant));

      // Q: How do I handle this side-effect?
      if (!err) {
        dispatch(extractUserInfo(grant));
      }
    });
  };
}

如果用户成功通过身份验证,我的 actionCreator 包含从授权中提取用户信息的业务逻辑;这个逻辑应该存在于我的动作创建者中吗?如果没有,我应该把它放在哪里,在我的减速器内?

在其他架构中,我会绑定一个命令来触发AUTHENTICATION_RESPONSE;但这感觉不像是中间件工作?

4

1 回答 1

3

我认为你的建议是完全明智的。
您可以将 Redux Thunk 用于控制流和副作用。
你永远不应该把副作用放在减速器中。

于 2015-10-10T11:32:22.133 回答