How can I api useQuery when calling a function?

const { data: allData, isLoading } = api.admin.limitAccess.useQuery( {first,rows}, { refetchOnWindowFocus: false } I am using useQuery to fetch first , rows depends on a pagination but I have a button to click so I can download all the data from the query so I want to use it like this const exportCSV = async () => { try { const response = await api.admin.limitAccess.useQuery({ first: 0, rows: totalRecords }); setAllData(response?.data); tableRef?.current?.exportCSV(); } catch (error) { console.error("Error exporting CSV:", error); } }; but when I use something like this I get an error , why I cannot use the api inside a function ? Error exporting CSV: Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app
8 Replies
Yiannis
Yiannis6mo ago
You should really study the react docs and in particular the rules of hooks. useQuery is a hook which means you may not call it inside functions that aren’t hooks themselves or after any conditional logic. https://react.dev/warnings/invalid-hook-call-warning I think to achieve what you want you can leverage the “enabled” api that Tanstack Query offers (TRPC uses TarackQuerry under the hood here). You may set it to a boolean flag of some kind and it will only trigger then: https://tanstack.com/query/latest/docs/react/guides/disabling-queries
Rules of Hooks – React
The library for web and native user interfaces
Disabling/Pausing Queries | TanStack Query Docs
If you ever want to disable a query from automatically running, you can use the enabled = false option. When enabled is false:
Yiannis
Yiannis6mo ago
If any of this isn’t clear feel free to let me know if I can help!
om3r
om3r6mo ago
thanks for reply @Yiannis , I read some about tanstack useQuery , I was using primereact datatable and i wanted to make it serverside paginating so I was using the ( skip - take ) to make the serverside pagination and it was working but when I was trying to download the CSV file to download all the files from the database , my question is , is there anyway to make skip - take easily while fetching all the data from the backend ?
rafaochoa
rafaochoa6mo ago
what do you mean with "skip - take easily"?
james162861
james1628615mo ago
@Yiannis Is it possible to modify the query so that the useQuery hook has {enabled : false} so that I can run it on button click?
Yiannis
Yiannis5mo ago
Yes the second parameter of the function is an object which has this key
Yiannis
Yiannis5mo ago
Disabling/Pausing Queries | TanStack Query Docs
If you ever want to disable a query from automatically running, you can use the enabled = false option. When enabled is false:
james162861
james1628615mo ago
@Yiannis, thanks for the help. Yes, setting the enabled parameter to a state that defaults to false and then having the onClick event set the state to true does the job.