-
Notifications
You must be signed in to change notification settings - Fork 0
/
redux.js
54 lines (43 loc) · 907 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
43
44
45
46
47
48
49
50
51
52
53
54
const redux = require('redux')
const initialState = {
counter: 0
}
// Reducer
const reducer = (state = initialState, action) => {
if (action.type === 'ADD') {
return {
counter: state.counter + 77
}
}
if (action.type === 'SUB') {
return {
counter: state.counter - 17
}
}
if (action.type === 'ADD_NUMBER') {
return {
counter: state.counter + action.tytyryty
}
}
return state
}
// Store
const store = redux.createStore(reducer)
console.log('0', store.getState())
store.subscribe(() => {
console.log("Subscribe", store.getState() )
})
// Actions
const addCounter = {
type: 'ADD'
}
const addCounter2 = {
type: 'ADD_NUMBER',
tytyryty: 777
}
store.dispatch(addCounter)
console.log('1', store.getState())
store.dispatch({ type: 'SUB' })
console.log('2', store.getState())
store.dispatch(addCounter2)
console.log('2', store.getState())