reactjs - 在使用 redux-observables 和 RxJS 获取史诗期间重定向
问题描述
如果用户无权访问服务器端资源,我有一个 fetch Epic 可能会返回 401 或 403。如果是这种情况,我想在这个 Epic 期间重定向。
现在我正在这样做:
export const fetch$ = <T = any>(req: IRequest) =>
from(performFetch<T>(req)).pipe(
switchMap(res => of(res)),
catchError(e => {
if (e.status === 401 || e.status === 403) {
window.location.replace(`/login?fwd=${encodeURIComponent(window.location.pathname)}`);
return NEVER;
}
return of(e);
})
);
WhereperformFetch
只是一个简单的函数,它执行 fetch 并返回一个 Promise。
我正在使用window.location.replace
,到目前为止它工作正常,但有人告诉我它会搞砸 React。
我尝试使用connected-react-router
并返回一个push
动作,但它没有进行重定向。
我可以安全地继续这样做,还是有更好的方法?
解决方案
不需要使用window.location.replace
so ,因为您已安装react-router
or connected-react-router
,它处理应用程序中的组件导航。
您可以考虑使用replace
或push
。
如果您希望返回操作,则必须使用of
运算符包装它。
import { replace } from 'connected-react-router'.
export const fetch$ = <T = any>(req: IRequest) =>
from(performFetch<T>(req)).pipe(
switchMap(res => of(res)),
catchError(e => {
if (e.status === 401 || e.status === 403) {
of(replace(`/login?fwd=${encodeURIComponent(window.location.pathname)}`));
}
return of(e);
})
);
或者,您可以简单地调用replace
/push
作为副作用,同时返回其他内容。
export const fetch$ = <T = any>(req: IRequest) =>
from(performFetch<T>(req)).pipe(
switchMap(res => of(res)),
catchError(e => {
if (e.status === 401 || e.status === 403) {
replace(`/login?fwd=${encodeURIComponent(window.location.pathname)}`);
return NEVER;
}
return of(e);
})
);
推荐阅读
- ios - React Native:设备中 HTTPS 的网络请求失败
- django - 如何确保单个模型只有一个 BooleanField 为真?
- python - 如何处理 biopython 翻译过程中的空白
- c# - 除了使用 -- 或 ++ 之类的运算符之外,您如何更改变量?
- python - 意外结果:pytest.raises with match parameter when asserting error
- css - 带阴影的发光盒效果
- javascript - 如何使用 jquery 将 bootstrap 4 导航项添加到活动的类中
- excel - sumif 如果条件是文本
- yacc - 编译基本 lex 和 yacc 计算器程序时出现多个错误
- swift - 如何 NSKeyedUnarchiver.unarchiveObject