Skip to content

Instantly share code, notes, and snippets.

@LambertSchulze
Last active August 9, 2020 01:28
Show Gist options
  • Save LambertSchulze/22daaf3c5feb65f0a60c5718787088ab to your computer and use it in GitHub Desktop.
Save LambertSchulze/22daaf3c5feb65f0a60c5718787088ab to your computer and use it in GitHub Desktop.
Redux Counter Example
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
const counterReducer = (state = 0, action) => {
switch(action.type) {
case INCREMENT:
return (state += 1);
case DECREMENT:
return (state -= 1);
default: return 0;
};
};
const incAction = () => {return {type: INCREMENT}};
const decAction = () => {return {type: DECREMENT}};
const store = Redux.createStore(counterReducer);
//store.dispatch(incAction());
@LambertSchulze
Copy link
Author

Example of a counter done with Redux:

  • action types: defined as const
  • reducer: takes state and action, returns state depending on action
  • action creators: return objects with the action type plus optional payload. They are as argument by dispatchers: store.dispatch(incAction())
  • Redux store: the state of the app, but called store...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment