我正在使用react
+ redux
+ redux-observable
。
我有以下史诗动作:
export const fetchCompaniesEpic = (action$: Observable<Action>): Observable<Action> =>
action$.pipe(
ofType(FETCH_COMPANIES),
take(1),
switchMap((action: any) => WebServiceProxy.getCompanies().pipe(map((companies: Company[]) => fetchCompaniesFulfilled(companies))))
);
先执行一些异步操作,然后使用结果调用“正常操作”:
export const fetchCompaniesFulfilled = (companies: Company[]) => ({ type: 'FETCH_COMPANIES_FULFILLED', companies });
然后在我的容器中,我以这种方式发送史诗动作:
const mapDispatchToProps: any = (dispatch: Dispatch<Action>, ownProps: any) => ({
fetchCompanies: (): void => {
dispatch({ type: FETCH_COMPANIES });
}
});
所有作品都像魅力一样,但是我怎么知道史诗般的动作何时结束?
我想做类似dispatch({ type: FETCH_COMPANIES }).subscribe(...)
答案 0 :(得分:0)
目前,我加强了以下解决方案,并发布了该解决方案,以便了解您对此的想法,并帮助遇到相同问题的其他人。
我实现了我想要的功能。
这是重击动作:
export const fetchCompaniesThunk = () => {
return (dispatch: any) => {
return fetchCompanies().pipe(map((companies: Company[]) => dispatch(fetchCompaniesFulfilled(companies))));
};
};
这是fetchCompaniesFulfilled
动作:
export const fetchCompanies = (): Observable<any> => {
return WebServiceProxy.getCompanies().pipe(take(1));
};
从容器中,我可以做:
dispatch(fetchCompaniesThunk()).subscribe((data: any) => {
console.log('finished to fetch companies', data);
});
即使解决方案运行良好,我也不是很满意。或者更好的是,我的实际问题是:为什么我需要redux-observable
?