Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API for clearing cache #28

Closed
cherniavskii opened this issue Nov 9, 2019 · 7 comments · Fixed by #31
Closed

API for clearing cache #28

cherniavskii opened this issue Nov 9, 2019 · 7 comments · Fixed by #31

Comments

@cherniavskii
Copy link
Contributor

The problem

There's a scenario, which is pretty common:

  • user logs in
  • some user data is fetched
  • user logs out
  • different user logs in and for a moment sees previous user's data

Normally, all user data should be deleted after log out, but there's no API for that at the moment.

I've tried setQueryData function to clear some queries, but it didn't work for me, since it doesn't make those query stale.

I've managed to clear cache like this:

import { queries } from 'react-query';

function handleLogout() {
    queries.length = 0;
}

But I'm not sure if it is safe to do this, since this is not a public API.

Proposed solution

Introduce clearQueries function, which will:

  • clear all queries if called without arguments
  • clear all queries except those provided as first argument.

The second option may be useful for applications, which are partly public. This will allow to clear user data cache, but keep public data cache.

Let me know do you think about that

@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 9, 2019 via email

@tannerlinsley
Copy link
Collaborator

Released in version 0.3.7 🎉

@cherniavskii
Copy link
Contributor Author

@tannerlinsley Thanks!

I've checked it, and it looks like clearing state.data might not be enough.
Consider case, when staleTime !== 0, and clearQueryCache is called.
Then, new data won't be fetched until query is marked as stale.

Looks like query state should be set to initial query state on clear:

https://github.com/tannerlinsley/react-query/blob/b89576713282351a9b620decb50145fc7a426c82/src/index.js#L64-L73

Also, stale timeout should be cleared as well.

What do you think? I can submit a PR

@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 12, 2019 via email

@tannerlinsley
Copy link
Collaborator

Actually. Just delete all of the entries all the time. If a query is rerun and can’t find a query, it will get instantiated as needed. No need to be specific I think.

@cherniavskii
Copy link
Contributor Author

Just delete all of the entries all the time

@tannerlinsley
Do you mean delete all state entries for every query or delete all entries of queries array?

@tannerlinsley
Copy link
Collaborator

tannerlinsley commented Nov 12, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants