2017-01-06 67 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

好的。謝謝(你的)信息。我想我在某處讀過。所有減速器都被調用並且它們的存儲輸出被合併。 –

相關問題