Skip to content

Instantly share code, notes, and snippets.

@jepras
Created October 17, 2018 16:32
Show Gist options
  • Save jepras/b3eceb4bca51a99d8563304020126134 to your computer and use it in GitHub Desktop.
Save jepras/b3eceb4bca51a99d8563304020126134 to your computer and use it in GitHub Desktop.
Used store.subscribe to invoke a callback function that simply adds +1 to count. Store.subscribe fires automatically when an action is dispatched.
const ADD = 'ADD';
const reducer = (state = 0, action) => {
switch(action.type) {
case ADD:
return state + 1;
default:
return state;
}
};
const store = Redux.createStore(reducer);
// global count variable:
let count = 0;
// change code below this line
store.subscribe( () => { ++count } )
// change code above this line
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
store.dispatch({type: ADD});
console.log(count);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment