使用Apollo客户端注销后重置存储 [英] Reset store after logout with Apollo client
问题描述
我试图在我的react-apollo应用程序注销后重置存储.
I'm trying to reset the store after logout in my react-apollo application.
因此,我创建了一个称为注销"的方法,当我单击按钮时调用该方法(并由"onDisconnect"道具传递).
So I've created a method called "logout" which is called when I click on a button (and passed by the 'onDisconnect' props).
为此,我尝试遵循以下示例: https://www.apollographql.com/docs/react/recipes/authentication.html
To do that I've tried to follow this example : https://www.apollographql.com/docs/react/recipes/authentication.html
但是在我的情况下,我希望LayoutComponent作为HOC(并且没有graphQL查询).
But in my case I want LayoutComponent as HOC (and it's without graphQL Query).
这是我的组成部分:
import React, {Component} from 'react';
import { withApollo, graphql } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import AppBar from 'material-ui/AppBar';
import Sidebar from 'Sidebar/Sidebar';
import RightMenu from 'RightMenu/RightMenu';
class Layout extends Component {
constructor(props) {
super(props);
}
logout = () => {
client.resetStore();
alert("YOUHOU");
}
render() {
return (
<div>
<AppBar title="myApp" iconElementRight={<RightMenu onDisconnect={ this.logout() } />} />
</div>
);
}
}
export default withApollo(Layout);
此处的问题是未定义客户端",因此我无法正确注销. 您是否有任何想法可以帮助我处理这种情况,或者有一个示例/最佳实践来从apollo客户端注销?
The issue here is that 'client' is not defined and I can't logout properly. Do you have any idea to help me to handle this situation or an example/best practices to logout from apollo client ?
多谢
推荐答案
如果您需要清除缓存并且不想获取所有活动查询,则可以使用:
If you need to clear your cache and don't want to fetch all active queries you can use:
client.cache.reset()
client
是您的Apollo客户.
client
being your Apollo client.
请记住,这不会触发onResetStore
事件.
Keep in mind that this will NOT trigger the onResetStore
event.
这篇关于使用Apollo客户端注销后重置存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!