redux 代码

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
import { createStore} from 'redux'
//新建 store 通过 reducer 建立。
// 根据旧的state和 action 生成新的state
function counter(state = 0,action){
switch(action.type){
case 'A':
return state+1
case "B":
return state-1
default :
return 10;
}
}
// 新建 store
const store=createStore(counter);
const init = store.getState();
console.log(init);
//订阅事件
function listener(){
const current = store.getState();
console.log(`${current}`)
}
store.subscribe(listener);
//派发事件 传递action
store.dispatch({type:"A"})
store.dispatch({type:"A"})
store.dispatch({type:"A"})