-
Notifications
You must be signed in to change notification settings - Fork 14
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
atomWithSuspenseQuery
- implementation changes suggestion
#72
Comments
You are amazing! Suspense Queries not being able to reset error boundaries has been one of the most annoying bugs I've ever seen. I've gone through the explanation in your comments and the working example you've linked. I haven't tested the code yet but I'll do that this week. Meanwhile if you want to raise a PR feel free to do so, and I can add my comments from my local testing if any. |
I was hoping to not create a new atom and have it as a part of the existing atomWithSuspenseQuery if possible but I couldn't spend a lot of time on this. I would love to see your idea. |
Just wanted to open an issue about the types being wrong as enabled is still allowed in a atomWithSuspenseQuery. Which it shouldn't be. But I see you fixed that as well, nice! |
@kalijonn sorry for the two weeks silence 🙈 I will create a PR in next few days. |
@grzesiek-ds Sick! |
@feledori I think it's because of type widening. In some cases, typescript doesn't care about additional type parameters. Something like this: https://stackoverflow.com/questions/54977550/what-is-widening-of-a-function-return-type-in-typescript Omit exists in the existing implementation but doesn't throw an error unless you cast it explicitly. const userAtom = atomWithSuspenseQuery<object>((get) => ({
queryKey: ['users', get(idAtom)],
queryFn: async ({ queryKey: [, id] }) => {
const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`)
return res.json()
},
enabled: false, //no error
}))
const userAtom2 = atomWithSuspenseQuery<object>((get) => {
return {
queryKey: ['users', get(idAtom)],
queryFn: async ({ queryKey: [, id] }) => {
const res = await fetch(
`https://jsonplaceholder.typicode.com/users/${id}`
)
return res.json()
},
enabled: false, // error - Object literal may only specify known properties, and 'enabled' does not exist in type 'AtomWithSuspenseQueryOptions<unknown, Error, unknown, QueryKey>'
} satisfies AtomWithSuspenseQueryOptions
}) |
Hello!
First of all, thanks for creating
jotai-tanstack-query
, it's a great alternative for@tanstack/react-query
which allows us to manage data fetching directly via atoms.Problem statement
Consider a query which did not manage to fetch data after first mount (therefore it throws an error and triggers
ErrorBoundary
).After resetting
ErrorBoundary
,atomWithSuspenseQuery
is still in error state, and it's not possible to "automatically" reset it.I believe that this is same issue as #32.
I realize that theres an example which uses another atom, which can be updated in order to recalculate
atomWithSuspenseQuery
, but this approach does not scale well.codesandbox
with example of issue and proposed solutionTheres
codesandbox
which has two examples -BrokenExample
andFixedExample
.First one is example of the issue, while second one is using modified implementation of
atomWithSuspenseQuery
and appears to work correctly.You can switch between these examples in
App.tsx
file.I've left bunch of comments in
fixedAtomWithSuspenseQuery
file, explaining the approach. This atom is also available in snippet below in order to allow easier discussion on github.Proposed solution
fixedAtomWithSuspenseQuery snippet
Since english is not my main language, I suspect that some comments could be unclear, let me know if that's the case please.
In addition:
0.8
feature - async query options getter, Dependent Suspense Queries #65, I'm willing to work on it if You will consider my approach as valid.throwOnError
in order to resolve a specific case that I have. But my suggested change touseSuspenseQuery
got rejected, therefore I am going to revert these changes. Ignore that for now please.useSuspenseQuery - throw error while having stale data TanStack/query#6960
The text was updated successfully, but these errors were encountered: