Effect
- Effect 是一个处理所有副作用的函数。它接收下面的参数
- Action action
- Context context
- BuildContext context
- T state
- dispatch
- isDisposed
- 它主要包含四方面的信息
- 接收来自 View 的“意图”,包括对应的生命周期的回调,然后做出具体的执行。
- 它的处理可能是一个异步函数,数据可能在过程中被修改,所以我们应该通过 context.state 获取最新数据。
- 如果它要修改数据,应该发一个 Action 到 Reducer 里去处理。它对数据是只读的,不能直接去修改数据。
Effect<String> buildEffect() {
return combineEffects(<Object, Effect<String>>{
Lifecycle.initState: _initState,
'onShare': _onShare,
});
}
//do something on initState
void _onShare(Action action, Context<String> ctx) async {
//do something on onShare
await Future<void>.delayed(Duration(milliseconds: 1000));
ctx.dispatch(const Action('shared'));
class MessageComponent extends Component<String> {
MessageComponent(): super(
view: buildMessageView,
effect: buildEffect(),
}