React Redux使用
# 基本概念
- store state
- action
- reducer
# 单项数据流
- dispatch(action)
- reducer -> newState
- subscribe 触发通知
# react-redux
- connect
- mapStateToProps mapDispatchToProps
# 异步action
- redux-thunk
- redux-promise
- redux-saga
# 中间件
# 示例
import { applyMiddleware, createstore } from ' redux";
import createLogger from redux-Logger';
import thunk from redux-thunk:
const Logger = createLogger();
const store = createStore(
reducer,
applyMiddleware(thunk, logger) // 会按顺序执行
)
//自己修改 dispatch,增加 Logger
Let next = store.dispatch;
store.dispatch = function dispatchAndLog (action) {
console. Log('dispatching', action):
next (action);
console. log('next state', store.getState());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
上次更新: 2022/08/14, 18:25:44