Skip to content

Instantly share code, notes, and snippets.

@NEO97online
Created July 8, 2019 01:20
Show Gist options
  • Save NEO97online/92a1684a8fe2f697745317fe383698c2 to your computer and use it in GitHub Desktop.
Save NEO97online/92a1684a8fe2f697745317fe383698c2 to your computer and use it in GitHub Desktop.
Simplified action type creator with constant validation
import camelCase from 'lodash/camelCase'
export default function createActions(actionTypes, actionTypeConstants) {
const actions = {}
for (const actionType of actionTypes) {
if (actionTypeConstants && !actionTypeConstants[actionType]) {
throw new Error(`Tried creating invalid action type: ${actionType}`)
}
const key = camelCase(actionType)
actions[key] = (payload) => ({
type: actionType,
payload
})
}
return actions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment