2017-01-06 28 views
1

我用终极版的combineReducers()辅助函数两个减速结合,像这样:终极版:嵌套减速访问另一个存储部分

const rootReducer = combineReducers({ 
    user: userReducer, 
    accounts: accountsReducer 
}) 

我知道每个减速只能修改它分配给店里一片,“用户“和”账户“。

我如何从我的账户减速器修改我店面的“用户”部分?

回答

2

你不能。

您可以收听到两个减速器相同的动作,或者如果你需要更新基础上,更新到账户的状态,用户状态,那么你可以使用终极版的thunk - https://github.com/gaearon/redux-thunk

const action =() => (dispatch, getState) => { 
    // dispatch action that accounts reducer listens to 
    dispatch({ 
    type: 'SOME_ACTION' 
    }) 

    // get the new accounts state 
    let { accounts } = getState() 

    // dispatch action that user reducer listens to, passing the new accounts state 
    dispatch({ 
    type: 'ANOTHER_ACTION', 
    payload: { 
     accounts 
    } 
    }) 
} 

// call with 
dispatch(action()) 
+0

“在两个缩减器中听同样的动作“ - 这是否意味着我可以这样做:派发操作,例如{type:'SET_ACTIVE_ACCOUNT',data:data}然后在账户中减速器处理设置活动账户和用户的操作reducer听相同的事件并修改商店的某些部分? –

+0

@GilbertNwaiwu是的,当一个动作被调度时,每个reducer都会被调用。你可以从1动作更新多个reducer的状态(前提是你的动作有效载荷中有你需要的所有数据) –

+1

好的。谢谢(你的)信息。我想我在某处读过。所有减速器都被调用并且它们的存储输出被合并。 –

相关问题