Skip to content

Add a skipToken that resets the query hook if it's been initialized before (e.g. skipOrResetToken) #2411

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

Closed
kasparkallas opened this issue Jun 14, 2022 · 5 comments

Comments

@kasparkallas
Copy link

kasparkallas commented Jun 14, 2022

I'd like to see an easy way to reset the query to uninitialized without having to re-mount the component.

Example usage:

const accountData = useAccountDataQuery(accountId ? { id: accountId } : skipOrResetToken);

Passing the skipOrResetToken guarantees that there's no data (i.e. stale data) coming from the query.

The use-case arose from the fact that the stale data with just the skipToken can be surprising when you have a user inputted conditional for whether to execute the query or not (e.g. "address", "item name", "username" etc).

@markerikson
Copy link
Collaborator

Seems plausible. I also don't think we have a way to reset a single cache entry imperatively atm - you can reset the entire API state, but not just one entry.

@phryneas
Copy link
Member

I think the point here is more to reset the hook rather than the cache entry, right?

@kasparkallas
Copy link
Author

kasparkallas commented Jun 14, 2022

I think the point here is more to reset the hook rather than the cache entry, right?

Yes, it's about resetting the hook -- I'll edit the title.

Here's a more detailed example of the type of code I currently end up writing:

const { connectedAccount } = useAccountContext();
const accountSettingsQuery = useAccountSettingsQuery(connectedAccount ? { account: connectedAccount } : skipToken);
const accountSettings = connectedAccount ? accountSettingQuery?.data : undefined;

// and then render something based on the settings
  1. Most of the time the component is landed on without a connected account so the query result is uninitialized as expected with undefined data.
  2. Then the account is connected and the query has the account related data.
  3. And then the account is disconnected and I would like to see undefined data again but in actuality I get the last result.

There's an argument to be made that I should just re-mount the component for such a context change (fair). Or that the extra variable solution is simple (which it is) but the main issue with it is that it's easy to miss. I mistakenly tend to take for granted that when skipToken is passed then I will surely not have any data result.

@kasparkallas kasparkallas changed the title Add a skipToken that resets the query if it's been initialized before (e.g. skipOrResetToken) Add a skipToken that resets the query hook if it's been initialized before (e.g. skipOrResetToken) Jun 14, 2022
@markerikson
Copy link
Collaborator

@kasparkallas hmm. Would it help if you checked accountSettingsQuery.currentData instead? I think that should be "the value from the absolute latest argument", whereas data is "the last successful value".

@kasparkallas
Copy link
Author

kasparkallas commented Jun 14, 2022

kasparkallas hmm. Would it help if you checked accountSettingsQuery.currentData instead? I think that should be "the value from the absolute latest argument", whereas data is "the last successful value".

Looks like that is indeed the case... 😛 Thank you very much, sir!

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

No branches or pull requests

3 participants