Created
April 4, 2018 23:44
-
-
Save mtho11/aa50b1453a8180ce66d972055d21527e to your computer and use it in GitHub Desktop.
Sample redux program.
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'); | |
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