【发布时间】:2023-01-20 18:44:47
【问题描述】:
我有两只鸭子(ui 和后端),里面有史诗。 完成后端操作后,我需要触发两个动作 其中一个动作位于后端鸭子中,另一个位于 ui 鸭子中。
我从后台操作开始,一切都按预期进行。 添加第二个操作会导致我遇到问题,因为我可以访问操作(控制台正确记录),但不能访问 reducer(无日志)
我要解决的挑战是:
- 在一个史诗中开始两个动作
- 在另一个减速器中调度一个动作
我的代码看起来与此类似:
backendDuck 的史诗:
fetchFooEpic : (action$, store) =>
action$.pipe(
operators.filter(action => action.type === types.LOAD),
// start added section for second call
operators.switchMap(action => {
const response = operators.from(fetchSomeUrl(action))
.pipe(
operators.of(uiDuck.actions.fetchUserFulfilled(response.props)),
),
operators.catchError(err => {
console.error('Error happened!', err.message)
return rxjs.of({ type: types.ADD_CATEGORY_ERROR, payload: err })
})
return response
}),
// start added section for second call
// original first call
operators.map(a => ({ type: types.ENDACTION, payload: a.payload })),
operators.catchError(err => {
console.error('Error happened!', err.message)
return rxjs.of({ type: types.ADD_CATEGORY_ERROR, payload: err })
})
)
uiDuck:
export actions={
...
fetchUserFulfilled: (value) => {
console.log('hello from action')
return ({ type: types.FETCHUSERFULFILLED, payload: value })
},
...
}
...
export default function reducer(state = initialState, action) {
switch (action.type) {
case types.FETCHUSERFULFILLED:
console.log('hello from reducer')
return {
...state,
user: action.payload,
}
...
【问题讨论】:
标签: redux-observable