- 當Application越來越複雜,我們可能會想要創造出不只一個的Reducer以管理state的不同部分。下面是一個state的範例,我們可能想要一個Reply Reducer只處理state中replies的部分,而User Reducer只處理state中users的部分。
{ users: {}, modal: {}, posts: {}, replies: {}, listeners: {} }
- 想要達成的話,我們在createStore的時候就不會只帶入一個Reducer,而是把所有的Reducer import成一個物件,然後使用combineReducers()將這個物件轉成一個reducing function,最後再將這個reducing function帶入createStore。
import combineReducers from 'redux' import * as reducers from '../reducers ... const store = createStore(combineReducers(reducers))
- 從combineReducers回傳的main reducer會呼叫每一個child reducer,並且收集它們的結果組合成一個state物件。
沒有留言:
張貼留言