Redux Observable - RXjs - If 语句
Redux Obserable - RXjs - If statement
好吧,我有一个嵌套在另一个管道中的管道,我想映射一个可观察对象并提供一个 if 语句,如果为真,return 一个动作,如果不是,则 return另一个动作
举个例子,我想做这个
export const itemAddRequest = (action$, state$) => {
const newState$ = from(state$.value.ids);
return action$.pipe(
ofType("ADD_ITEM_REQUEST"),
mergeMap(action =>
newState$.pipe(
map(value => {
if (value === action.payload.id) {
mapTo(addItemSuccess());
} else {
mapTo(addItemFailed());
}
})
)
)
);
};
大家有什么想法吗?
这很简单。您可以直接从 map()
运算符 return 操作,因为 redux-observable
需要获得 Observable<Action>
作为最终值。
export const itemAddRequest = (action$, state$) => {
const newState$ = from(state$.value.ids);
return action$.pipe(
ofType("ADD_ITEM_REQUEST"),
mergeMap(action =>
newState$.pipe(
map(value => {
return value === action.payload.id
? addItemSuccess()
: addItemFailed();
})
)
)
);
};
好吧,我有一个嵌套在另一个管道中的管道,我想映射一个可观察对象并提供一个 if 语句,如果为真,return 一个动作,如果不是,则 return另一个动作
举个例子,我想做这个
export const itemAddRequest = (action$, state$) => {
const newState$ = from(state$.value.ids);
return action$.pipe(
ofType("ADD_ITEM_REQUEST"),
mergeMap(action =>
newState$.pipe(
map(value => {
if (value === action.payload.id) {
mapTo(addItemSuccess());
} else {
mapTo(addItemFailed());
}
})
)
)
);
};
大家有什么想法吗?
这很简单。您可以直接从 map()
运算符 return 操作,因为 redux-observable
需要获得 Observable<Action>
作为最终值。
export const itemAddRequest = (action$, state$) => {
const newState$ = from(state$.value.ids);
return action$.pipe(
ofType("ADD_ITEM_REQUEST"),
mergeMap(action =>
newState$.pipe(
map(value => {
return value === action.payload.id
? addItemSuccess()
: addItemFailed();
})
)
)
);
};