Skip to content

Instantly share code, notes, and snippets.

@clemlatz
Created February 6, 2018 12:31
Show Gist options
  • Save clemlatz/fbc1ebd1a6aa4122835c856d4b98b2b3 to your computer and use it in GitHub Desktop.
Save clemlatz/fbc1ebd1a6aa4122835c856d4b98b2b3 to your computer and use it in GitHub Desktop.
Redux basics
const redux = require('redux');
const createStore = redux.createStore;
const initialState = {
counter: 0,
};
// Reducer
const rootReducer = (state = initialState, action) => {
if (action.type === 'INC_COUNTER') {
return { ...state, counter: state.counter + 1 };
}
if (action.type === 'ADD_COUNTER') {
return { ...state, counter: state.counter + action.value };
}
return state;
};
// Store
const store = createStore(rootReducer);
console.log(store.getState());
// Subscription
store.subscribe(() => {
//console.log('[subscription]', store.getState());
});
// Dispatching action
store.dispatch({ type: 'INC_COUNTER' });
store.dispatch({ type: 'ADD_COUNTER', value: 10 });
console.log(store.getState());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment