Created
February 26, 2018 09:01
-
-
Save char0n/7c7bed08ddffa2958e7fe842e234cded to your computer and use it in GitHub Desktop.
Configurable reducers - https://blog.apiary.io/2018/02/12/Reusable-features-in-React-Redux#comment-3776553185
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // in reusable feature instance | |
| import createActions from '../reusable-feature/actions'; | |
| import selectors from './selectors'; | |
| import { createAction } from 'redux-actions'; | |
| const actions = createActions('INSTANCE_1', selectors); | |
| actions['additionalAction'] = createAction(...); | |
| export default actions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // in reusable feature template | |
| import { handleActions } from 'redux-actions'; | |
| export default function createReducers(actions) { | |
| const initialState = { | |
| clickCount: 0, | |
| }; | |
| const handlers = { | |
| [actions.incrementClick]: state => ({ ...state, clickCount: state.clickCount + 1 }), | |
| }; | |
| if (action.additionalAction) { | |
| handlers[actions.additionalAction] = state => ({ ... }); | |
| } | |
| return handleActions(handlers, initialState); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment