Skip to content

Instantly share code, notes, and snippets.

@jurgob
Last active March 19, 2016 00:29
Show Gist options
  • Save jurgob/1c95e20318c217dc54d6 to your computer and use it in GitHub Desktop.
Save jurgob/1c95e20318c217dc54d6 to your computer and use it in GitHub Desktop.
react utils - the utils
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