I created custom hooks for get requests from the server. Below is an example
const usePost = (id: string) => {
return useQuery(['posts', id], () => getPost(id))
}
const usePosts = () => {
return useQuery(['posts'], () => getPosts())
}
However, I sometimes need to call multiple queries in a single file, so I would like to use the useQueries hook along with the custom hooks. But when I try to use the custom hooks as the queryFn, React throws me an error saying I can't call a hook inside the function 'queryFn'.
const queries = useQueries([
{
queryKey: 'posts',
queryFn: () => usePosts
},
{
queryKey: ['post', id],
queryFn: () => usePost(id)
}
])
// Failed to compile, 'usePost' is called in function 'queryFn' that is neither a React component nor custom hook
How would we be able to use useQueries with custom query hooks?
react-queryworks, but you're calling theusePosthook in the second object, which is not allowed in React outside of a component render function. Note that, in your code, the firstqueryFnis a 'function that returns a function', while the otherqueryFnis not. I thinkqueryFn: () => () => usePost(id)might be what you want.