Redux 
- 单一数据源 
- state 是只读的 
- 使用纯函数来执行修改 
- createStore 可以帮助创建 store 
- store.dispatch 帮助派发 action , action 会传递给 store 
- store.getState 这个方法可以帮助获取 store 里边所有的数据内容 
- store.subscrible 方法订阅 store 的改变,只要 store 发生改变, store.subscrible 这个函数接收的这个回调函数就会被执行 
jsx
const redux = require('redux');
const initialState = {
  counter: 0
}
// 创建reducer
const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "INCREMENT":
      return {...state, counter: state.counter + 1};
    case "DECREMENT":
      return {...state, counter: state.counter - 1};
    case "ADD_NUMBER":
      return {...state, counter: state.counter + action.number}
    default: 
      return state;
  }
}
// 根据reducer创建store
const store = redux.createStore(reducer);
store.subscribe(() => {
  console.log(store.getState());
})
// 修改store中的state
store.dispatch({
  type: "INCREMENT"
})
// console.log(store.getState());
store.dispatch({
  type: "DECREMENT"
})
// console.log(store.getState());
store.dispatch({
  type: "ADD_NUMBER",
  number: 5
})
// console.log(store.getState());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
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
redux 中间件,如:
- redux-thunk:用于异步操作
- redux-logger:用于日志记录
jsx
const store = createStore(
  reducer,
  applyMiddleware(thunk, logger)
);1
2
3
4
2
3
4