Reactjs Redux observable:当操作完成时,组件如何响应?
假设此演示代码:Reactjs Redux observable:当操作完成时,组件如何响应?,reactjs,redux,rxjs,redux-observable,Reactjs,Redux,Rxjs,Redux Observable,假设此演示代码: const pingEpic = action$ => action$.pipe( filter(action => action.type === 'PING'), delay(1000), // Asynchronously wait 1000ms then continue mapTo({ type: 'PONG' }) ); // later... dispatch({ type: 'PING' }); const pingReducer =
const pingEpic = action$ => action$.pipe(
filter(action => action.type === 'PING'),
delay(1000), // Asynchronously wait 1000ms then continue
mapTo({ type: 'PONG' })
);
// later...
dispatch({ type: 'PING' });
const pingReducer = (state = {}, action) => {
switch (action.type) {
case 'PING':
return state;
case 'PONG':
return state;
default:
return state;
}
};
在一个特定的组件中,假设与调度PING或PONG无关,也不使用任何redux状态,我希望以某种方式订阅操作生命周期,并且当PONG操作完成(即已由reducer处理)时,它执行回调。比如:
const myComponent = () => {
ofActionSuccessful('PONG').subscribe( () => console.log('PONG has just completed'));
}
比如:
我怎样才能做到这一点
我不想在reducer中链接一些逻辑,因为它与该组件严格相关,与存储无关。来自:
Epics在还原程序已经接收到它们之后,沿着正常的Redux调度通道运行——因此您不能“吞下”传入的操作。行动总是在你的史诗收到它们之前通过你的减速器
所以我们可以用epics来获取“乒乓”信号
const allActions$=new Subject();
//这必须合并到你的根史诗。
导出常量tapAllActions=(操作$)=>
动作$.pipe(tap(allActions$),concatMapTo(EMPTY));
导出const of ActionSuccessful=(actionType)=>
allActions$.pipe(过滤器(({type})=>type==actionType));