Created
January 21, 2020 03:30
-
-
Save onewoorks/f5dcb01eb38a88380151d4c3c598dded to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = { | |
| value: 0, | |
| age: 17 | |
| } | |
| // 2. reducer -- laluan untuk update value dalam store | |
| const rootReducer = (state = initialState, action) => { | |
| if (action.type === 'ADD_AGE'){ | |
| return { | |
| ...state, | |
| age: state.age + 1 | |
| } | |
| } | |
| if (action.type === 'CHANGE_VALUE'){ | |
| return { | |
| ...state, | |
| value: state.value + action.value | |
| } | |
| } | |
| return state; | |
| } | |
| // 1. store -- tempat simpan value | |
| const store = createStore(rootReducer) | |
| console.log(store.getState()) | |
| // 4. Subscription -- process panggil store | |
| store.subscribe(()=>{ | |
| console.log('store change = ', store.getState()) | |
| }) | |
| // 3. dispatch(action) -- process panggil reducer | |
| store.dispatch({ | |
| type: 'ADD_AGE', | |
| age: 1 | |
| }) | |
| store.dispatch({ | |
| type: 'CHANGE_VALUE', | |
| value: 20 | |
| }) | |
| console.log(store.getState()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment