-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux.js
42 lines (34 loc) · 868 Bytes
/
redux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const redux = require('redux');
const createStore = redux.createStore;
const initialState = {
value: 0,
age: 19
}
//reduce
const rootReducer = (state = initialState, action) => {
switch (action.type) {
case 'ADD_AGE':
return {
...state,
age: state.age + 1
}
case 'CHANGE_VALUE':
return {
...state,
value: action.newValue
}
default:
return state;
}
}
// 1. store(wadah besar untuk menyimpan state)
const store = createStore(rootReducer);
console.log(store.getState());
store.subscribe(() => {
console.log('result change', store.getState());
})
//dispatch
store.dispatch({ type: 'ADD_AGE' });
store.dispatch({ type: 'CHANGE_VALUE', newValue: 13 });
console.log(store.getState());
//subcription