使用 Apollo 客户端注销后重置商店 [英] Reset store after logout with Apollo client

查看:35
本文介绍了使用 Apollo 客户端注销后重置商店的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 react-apollo 应用程序中注销后重置商店.

所以我创建了一个名为logout"的方法,当我点击一个按钮时调用它(并通过onDisconnect"道具传递).

为此,我尝试按照以下示例进行操作:https://www.apollographql.com/docs/react/recipes/authentication.html

但在我的情况下,我希望 LayoutComponent 作为 HOC(并且它没有 graphQL 查询).

这是我的组件:

import React, {Component} from 'react';从反应阿波罗"导入 { withApollo, graphql };从'apollo-client'导入{ ApolloClient};从'material-ui/AppBar'导入AppBar;从侧边栏/侧边栏"导入侧边栏;从'RightMenu/RightMenu'导入RightMenu;类布局扩展组件{构造函数(道具){超级(道具);}注销 = () =>{客户端.resetStore();警报(你");}使成为() {返回 (<div><AppBar title="myApp" iconElementRight={<RightMenu onDisconnect={ this.logout() }/>}/>

);}}导出默认 withApollo(Layout);

这里的问题是未定义客户端",我无法正确注销.您有什么想法可以帮助我处理这种情况或从 apollo 客户端注销的示例/最佳实践吗?

提前致谢

解决方案

如果您需要清除缓存并且不想获取所有活动查询,您可以使用:

client.cache.reset()

client 是您的 Apollo 客户端.

请记住,这不会触发 onResetStore 事件.

I'm trying to reset the store after logout in my react-apollo application.

So I've created a method called "logout" which is called when I click on a button (and passed by the 'onDisconnect' props).

To do that I've tried to follow this example : https://www.apollographql.com/docs/react/recipes/authentication.html

But in my case I want LayoutComponent as HOC (and it's without graphQL Query).

Here is my component :

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);

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 ?

Thanks by advance

解决方案

If you need to clear your cache and don't want to fetch all active queries you can use:

client.cache.reset()

client being your Apollo client.

Keep in mind that this will NOT trigger the onResetStore event.

这篇关于使用 Apollo 客户端注销后重置商店的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆