我正在编写一个使用的React应用,apollo-client并且正在apollo-link-error全局捕获身份验证错误。我正在使用createBrowserHistory浏览器历史记录操纵和redux管理我的应用状态。
出现身份验证错误时,我想将用户重定向到/login页面。但是,使用history.push('/login')and forceRefresh:false可以更改URL,但实际上不会在我的应用程序内部导航。
如果我使用forceRefresh:true它可以正常工作,但该应用程序已完全重启,我想避免这种情况。
const errorLink = onError(({ graphQLErrors, networkError }) => {
if(graphQLErrors[0].extensions.code == "UNAUTHENTICATED") {
// with forceRefresh:true this works, but causes a nasty
// reload, without the app doesn't navigate, only the url changes
history.push('/login')
}
});
Run Code Online (Sandbox Code Playgroud)
`
let links = [errorLink, authLink, httpLink];
const link = ApolloLink.from(links);
const client = new ApolloClient({
link: link,
cache: new InMemoryCache(),
connectToDevTools: true,
});
Run Code Online (Sandbox Code Playgroud)
我认为问题是我没有使用redux-router …