Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Created February 13, 2020 17:33
Show Gist options
  • Select an option

  • Save r3dm1ke/3384a67a6e08c3a51687d944af42752e to your computer and use it in GitHub Desktop.

Select an option

Save r3dm1ke/3384a67a6e08c3a51687d944af42752e to your computer and use it in GitHub Desktop.
Simple redux example
const {createStore} from 'redux';
// this is how state looks initially
const INITIAL_STATE = {
counter: 0
};
function reducer(state=INITIAL_STATE, action) {
// this function will return a new state
// based on action
switch (action.type) {
case 'INCREMENT':
return {...state, counter: state.counter + 1};
case 'DECREMENT':
return {...state, counter: state.counter - 1};
default:
return state;
}
}
// createStore function is exported from redux and is used
// to create a redux store
const store = createStore(reducer);
store.dispatch({type: 'INCREMENT'}); // counter = 1 now
store.dispatch({type: 'INCREMENT'}); // counter = 2 now
store.dispatch({type: 'DECREMENT'}); // counter = 1 now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment