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

useQuery wrapper calls API twice #300

Open
pedropmedina opened this issue Jul 11, 2023 · 10 comments
Open

useQuery wrapper calls API twice #300

pedropmedina opened this issue Jul 11, 2023 · 10 comments

Comments

@pedropmedina
Copy link

I'm not sure if anyone else is experiencing this issue, but I have tried several solution and nothing works. It seems as if the useQuery wrapper makes two calls over the network. This issue is NOT present when using react-query's native useQuery hook and calling the api with the ts-rest regular client as the queryFn.

Here's what I'm seeing

Screenshot 2023-07-11 at 12 07 08 AM
export const api = initQueryClient(contract, {
 baseUrl: `${process.env.NEXT_PUBLIC_AUTH_API_URL}`,
 jsonQuery: true,
 baseHeaders: {},
 credentials: 'include',
 
 //  This makes two calls
   const { data, isInitialLoading } = api.application.findMany.useQuery(QUERY_KEY, {
   query: { take: 100 },
 });

// This one makes a single call
 const { data, isInitialLoading } = useQuery({
   queryKey: QUERY_KEY,
   queryFn: async () => {
     const res = await api.application.findMany.query({ query: { take: 100 } });
     return res.status === 200 ? res : null;
   },
 });
});
@michaelangeloio
Copy link
Contributor

@pedropmedina is it possible for you to provide a minimal repo for us to reproduce the behavior?

@danielpza
Copy link

perhaps this is caused by react strict mode on development? https://react.dev/reference/react/StrictMode#fixing-bugs-found-by-double-rendering-in-development

@pedropmedina
Copy link
Author

You are right @danielpza . The issue was strict mode. I'm using NextJs and turning it off got rid of the problem. However, doing so is not recommended. I'm not sure why this happens with the ts-rest wrapper and not when using react-query's native useQuery? Any thoughts?

@kurdin
Copy link

kurdin commented Jul 19, 2023

@pedropmedina
maybe this can help to explain it
facebook/react#24502 (comment)

@pedropmedina
Copy link
Author

I get what React is doing with strict mode @kurdin. I just don't understand why the ts-rest wrapper around react-query's userQuery is causing double fetching when this issue was already addressed by react-query's useQuery itself. In order words, using native useQuery triggers a single request while ts-rest's implementation triggers two. I think there's something in that helper that must be improved to avoid this behavior. At the end of the day, aren't we just calling native useQuery under the hood?

@kurdin
Copy link

kurdin commented Jul 19, 2023

You right @pedropmedina. I'll try to use ts-rest with react query to investigate this behaviour.

@oliverbutler
Copy link
Collaborator

Yeah, this is bizarre! We really do just return the underlying useQuery function with a provided queryFn - @michaelangelo-io any ideas or shall we write a test to investigate?

@michaelangeloio
Copy link
Contributor

@pedropmedina given the comments on this, TanStack/query#3492
do you see if the first request is aborted or not?

@pedropmedina
Copy link
Author

That's what I was expecting @michaelangelo-io, however both request go through the network successfully as shown in the image above ^. The weird thing is that this only happens when using ts-rest wrapper. Native useQuery works as expected.

@Talent30
Copy link

Talent30 commented Oct 4, 2023

@pedropmedina @kurdin Hi, there. I am happy to have a look if you can provide a minimal repo to reproduce the behaviour.

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

No branches or pull requests

6 participants