Created
May 31, 2022 12:01
-
-
Save DonzTea/4e5a181adda1d583ef3f5834d9b65cd6 to your computer and use it in GitHub Desktop.
plain redux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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