Last active
March 19, 2016 00:29
-
-
Save jurgob/1c95e20318c217dc54d6 to your computer and use it in GitHub Desktop.
react utils - the utils
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
function createReducer(initialState, handlers) { | |
return function reducer(state = initialState, action) { | |
if (handlers.hasOwnProperty(action.type)) { | |
return handlers[action.type](state, action) | |
} | |
else { | |
return state | |
} | |
} | |
} | |
export const createBasicReducer = (reducerName , initialState) => { | |
const _initialState = { | |
…initialState, | |
reducerName | |
} | |
return createReducer( | |
_initialState, | |
{ | |
[reducerName+’_SET_STATE’] : (state, action) => ({ | |
…state, | |
…action.state | |
} | |
) | |
})} | |
export const getReducerState = (store, reducerName) => { | |
return function(){ | |
const globalState = store.getState() | |
const stateReducerName = Object.keys(globalState).find((p) => globalState[p].reducerName === reducerName ) | |
return globalState[stateReducerName] | |
} | |
} | |
export const createUpdateStateAction = (reducerName) => { | |
return (state) => ({ | |
type:reducerName+’_SET_STATE’, | |
state | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment