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

How to reset atom to loading state after reseting an error boundary #32

Open
sandrosc opened this issue Apr 26, 2023 · 8 comments
Open
Labels
help wanted Extra attention is needed

Comments

@sandrosc
Copy link

So far I've tried using the QueryErrorResetBoundary (like described here) and calling queryClient.invalidateQueries(...) manually when reseting the error boundary, but the outcome is as follows:

  • I shut down my server & reload
  • The query fetches, fails, component enters error state, gets caught in error boundary
  • I click the fallback "try again" button to reset the error boundary
  • The component immediately renders in an error state and fails again
  • The query refetches, but doesn't seem to update the atom
  • Even after clicking "try again" again the same happens

The error handling example you provide seems to work by changing the user state to a different view where the query doesn't fail. When navigating to the item that causes the error, it immediately fails again. How could we reset the query, so instead of failing the atom tries again and reenters the loading state?

@dai-shi
Copy link
Member

dai-shi commented Apr 26, 2023

I'd admit the error boundary support is not ideal.

If you are using dataAtom (the first one from atomsWithQuery), it works like Jotai atoms and not like TanStack Query.
If you are using statusAtom (the second one), it works more like bare TanStack Query, but no error boundary support. (because it depends only on @tanstack/query-core not @tanstack/react-query.)

So, in either case, it's suboptimal.

@sandrosc
Copy link
Author

Thank you for the fast answer! So the best solution for now might be doing a window.reload() to cause a refetch of the entire page?

@dai-shi
Copy link
Member

dai-shi commented Apr 26, 2023

I wouldn't say reloading is best.
How about adding a dummy atom as a dependency?

@sandrosc
Copy link
Author

You mean like an dummy atom around the query atom that catches the error if it occurs?

@sandrosc
Copy link
Author

sandrosc commented Apr 26, 2023

Another question, do you think this library will work with the error boundary reset in the long run?

@dai-shi
Copy link
Member

dai-shi commented Apr 26, 2023

seems to work by changing the user state to a different view where the query doesn't fail.

By dummy atom, I mean something like the user state ☝️ .

do you think this library will work with the error boundary reset in the long run?

If it's about how Jotai handles error boundary, the functionality should be stable.
If it's about something very specific to this library, not sure. But, I think the current dataAtom is fairly a general Jotai atom.

That said, I agree there's some uncertainty, so using statusAtom would be a safer bet.

@sandrosc
Copy link
Author

If it's about something very specific to this library,

Yes I think it would be nice to integrate jotai and tanstack-query so that the case at the beginning of the issue can be handled easily. Handling user state separately defeats the purpose of the error boundary, which is supposed to be generic in my case.

@dai-shi
Copy link
Member

dai-shi commented Apr 26, 2023

I thought dataAtom and statusAtom could cover most cases, but they are still suboptimal.

We need maintainers with good understanding with tanstack/query-core and Jotai to revisit the API. (jotai-urql recently got a new maintainer and has a new API, dropping atomsWithQuery.)

Any volunteers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants