一、Redux的三个原则
- 遵循单一数据源的原则,只存在一个
Store
。 -
State
是只读的,只可以由dispatch
改变。 - 使用纯函数来执行修改,
reducer
是一个存函数。
(这三点在Flutter和React里面是一致的)
放个图,来源https://www.cnblogs.com/shuiyi/p/5081250.html
image.png
二、Flutter和React中Redux的异同点。
- 数据都是从上到下传递,React采用
Context
传递,Flutter采用InheritedWidget
传递。 -
action
用来判断执行reducer
的方法,React
中action
是一个包含type
属性的js对象,Flutter
中直接以类名作为判断。 -
React
中的combineReducers
是通过遍历小的reducer
执行合成一个大的Store
。
const combineReducers = reducers => {
return (state = {}, action) => {
return Object.keys(reducers).reduce(
(nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
},
{}
);
};
};
Flutter
中的combineReducers
也是通过类似的思想传入特定的action迭代完成。
Reducer<State> combineReducers<State>(Iterable<Reducer<State>> reducers) {
return (State state, dynamic action) {
for (final reducer in reducers) {
state = reducer(state, action);
}
return state;
};
}
三、react-redux
和flutter_redux
的异同
-
react-redux
通过connect
返回和Store
连接的组件,它会订阅dispatch
事件,每次dispatch
后会执行状态判断更新状态。 -
flutter_redux
通过StoreConnector
返回和Store
连接的组件,它也会通过StreamController
监听dispatch
事件,每次dispatch
后会执行状态判断更新状态。