Last active
September 4, 2019 11:37
-
-
Save furkanedu/1749a355517b6cc709c56c6195bdd599 to your computer and use it in GitHub Desktop.
Example redux file with constants, reducer and actions
This file contains 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
const namespace = 'shoud_be_unique_for_each_reducer' | |
const MERGE_REDUX_STATE_WITH = `${namespace}---MERGE_REDUX_STATE_WITH` | |
const RESET_REDUX_STATE = `${namespace}---RESET_REDUX_STATE` | |
const initialState = {} | |
export default function reducer(state = initialState, action) { | |
switch(action.type) { | |
case MERGE_REDUX_STATE_WITH: | |
return { | |
...state, | |
...action.data | |
} | |
case RESET_REDUX_STATE: | |
return { | |
...initialState | |
} | |
default: | |
return state | |
} | |
} | |
export function mergeReduxStateWith( objectToMerge = {} ){ | |
return function( dispatch, getState ){ | |
dispatch({ | |
type: MERGE_REDUX_STATE_WITH, | |
data : { ...objectToMerge } | |
}) | |
return Promise.resolve() | |
} | |
} | |
export function resetReduxState(){ | |
return function( dispatch, getState ){ | |
dispatch({ type: RESET_REDUX_STATE }) | |
return Promise.resolve() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment