【发布时间】:2019-06-17 04:13:06
【问题描述】:
我正在编写一个使用 apollo-client 的 React 应用程序,并且我正在使用 apollo-link-error 来全局捕获身份验证错误。我使用createBrowserHistory 进行浏览器历史操作,使用redux 管理我的应用状态。
在身份验证错误时,我想将用户重定向到 /login 页面。
但是,使用 history.push('/login') 和 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')
}
});
`
let links = [errorLink, authLink, httpLink];
const link = ApolloLink.from(links);
const client = new ApolloClient({
link: link,
cache: new InMemoryCache(),
connectToDevTools: true,
});
我认为问题在于我没有使用 redux-router 方法进行导航(因此即使 url 发生变化,应用程序也保持不变
问:当我不在组件内时,如何获得类似于使用withRouter() 的redux history 对象?处理这种情况的正确方法是什么?
【问题讨论】:
-
你想出解决方案了吗?
标签: reactjs react-apollo apollo-link