2015-10-06 30 views
0

我有一个异步actionCreator其处理我的应用程序的认证流程:处理副作用在异步(thunk'd)操作

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上触发;但是这不像中间件工作?

回答

3

我认为你的建议是完全明智的。
您可以使用Redux Thunk来控制流量和副作用。
你不应该把副作用放入减速器。

+0

赞赏,谢谢:) – JonnyReeves