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

useFunctionQuery from documentation cause infinite loop #81

Closed
powah opened this issue Dec 15, 2019 · 5 comments
Closed

useFunctionQuery from documentation cause infinite loop #81

powah opened this issue Dec 15, 2019 · 5 comments

Comments

@powah
Copy link

powah commented Dec 15, 2019

I liked the idea for key to use a function itself used for fetching the data because of no need to use strings or additional constants. Tried to use it from documentation and it triggers infinite rapid fetching. A function reference is stable (exported from separate module) so something inside custom hook needs to be memoized? Any guidance how to solve this?

I'm talking about this custom hook:

function useQuery(functionTuple, options) { const [queryFn] = Array.isArray(functionTuple) ? functionTuple : [functionTuple]; return useReactQuery(functionTuple, queryFn, options); }

@cherniavskii
Copy link
Contributor

You should stringify your function before using it as query key, since react-query checks if queryKey is a function, invokes it and uses returned value as key - see https://github.com/tannerlinsley/react-query/blob/master/src/index.js#L727.
In your case new Promise instance is used as query key, so new query is created on every render.

IMHO using strings or tuples (as suggested in docs) is much better option, but React Query isn't limited to it and of course you are free to use it as you want.

@powah
Copy link
Author

powah commented Dec 17, 2019

It's not a function, a query key is an array of function and variables [queryFn, variables]. Like I mentioned in an issue - that custom hook is take straight from react-query documentation:

// Heck, you can even make your own custom useQueryHook!

function useFunctionQuery(functionTuple, options) {
  const [queryFn, variables] = Array.isArray(functionTuple)
    ? functionTuple
    : [functionTuple]
  return useQuery(functionTuple, queryFn, options)
}

@cherniavskii
Copy link
Contributor

@powah do you also use custom query key serializer?

@powah
Copy link
Author

powah commented Dec 17, 2019

Oops, I guess now I understand that I have to do that. Will try and thanks for the help!

@cherniavskii
Copy link
Contributor

You're welcome :)

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