Last active
April 9, 2017 22:15
-
-
Save pokorson/bc774bf3135e27e8617250e5cc244b99 to your computer and use it in GitHub Desktop.
Redux helpers
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 createAction = (type) => { | |
return (payload) => ({type, payload}) | |
} | |
const createReducer = (initState, handlers) => { | |
return (state = initState, action) => { | |
const actionHandler = handlers[action.type]; | |
if (actionHandler) { | |
const result = actionHandler(state, action.payload); | |
return Object.assign({}, state, result); | |
} | |
return state; | |
}; | |
}; | |
// Example usage | |
const increment = createAction('INCREMENT'); | |
const postsReducer = createReducer({count: 0]}, { | |
'INCREMENT': (state, incrementNumber) => ({count: state.count + incrementNumber}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment