Skip to content

Instantly share code, notes, and snippets.

@whitelizard
Created November 18, 2019 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitelizard/4d0252175d1c25b9685eb11f9604ad75 to your computer and use it in GitHub Desktop.
Save whitelizard/4d0252175d1c25b9685eb11f9604ad75 to your computer and use it in GitHub Desktop.
Reducer for Immutable Store in JavaScript
const reducer = (state, action) => {
const actions = {
move: ({ id, payload }) => ({ ...state, [id]: setPos(state[id], payload) }),
hit: ({ id1, id2 }) => ({
...state,
[id1]: incHits(state[id1]),
[id2]: incHits(state[id2]),
}),
};
return actions[action.type](action);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment