Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

reactjs - Reset store after logout with Apollo client

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...