Skip to content

Instantly share code, notes, and snippets.

@memon07
Created April 29, 2019 08:22
Show Gist options
  • Save memon07/52d896582f408ef0641b198c0ba84197 to your computer and use it in GitHub Desktop.
Save memon07/52d896582f408ef0641b198c0ba84197 to your computer and use it in GitHub Desktop.
Simple redux working
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