Redux can I use one action type in separate reducers? -
i have situation in redux application have 3 separate reducers handle fetching of data api. example of 1 of reducers be:
const initial_state = { data: [], loading: false, error: '' }; export default (state = initial_state, action) => { switch (action.type) { case get_all_orders_start: return { ...state, loading: true }; case get_all_orders_success: return { ...state, allorders: action.payload, loading: false }; case get_all_orders_fail: return { ...state, loading: false, error: action.payload }; default: return state; } };
note loading , error states, these identical in each current reducer , subsequent reducers write involve fetching data api.
i add further reducer used loading , error pieces of state. other 3 store data.
this give me:
data reducers x 3
const initial_state = { data: [] // other state in future }; export default (state = initial_state, action) => { switch (action.type) { case get_all_orders_success: return { ...state, allorders: action.payload }; default: return state; } };
loading / error reducer (handles loading / error entire app)
const initial_state = { loading: false, error: '' }; export default (state = initial_state, action) => { switch (action.type) { case get_all_orders_start: return { ...state, loading: true }; case get_all_orders_success: return { ...state, loading: false }; case get_all_orders_fail: return { ...state, loading: false, error: action.payload }; default: return state; } };
as can see means get_all_order_success action type used in 2 separate reducers. question is, ok? or go against convention?
many in advance.
i think that's fine. there no place states actions , reducers have 1:1 mapping. in fact, creator of redux explicitly says there no relation between them, many reducers can react single actions, single reducer can react multiple actions.
i think says best: https://github.com/reduxible/reduxible/issues/8
tweet: https://twitter.com/dan_abramov/status/691608868628119552
relevant so: redux: why not put actions , reducer in same file?
Comments
Post a Comment