Skip to content

Instantly share code, notes, and snippets.

@DonzTea
Created May 31, 2022 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonzTea/4e5a181adda1d583ef3f5834d9b65cd6 to your computer and use it in GitHub Desktop.
Save DonzTea/4e5a181adda1d583ef3f5834d9b65cd6 to your computer and use it in GitHub Desktop.
plain redux
const redux = require('redux');
// * declaring initial state
const initialState = {
counter: 0,
};
// * reducer
const rootReducer = (state = initialState, action) => {
if (action.type === 'INC_COUNTER') {
state.counter += 1;
} else if (action.type === 'ADD_COUNTER') {
state.counter += action.value;
}
return state;
};
// * store
const store = redux.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