how to refetch with a passed in variable, state is behind
I'm strugging with the best way to handle this. I have a button in my web app that generates an Excel spreadsheet in the backend and sends it to the browser as a download. When the button is clicked, it passes an id to a function so it knows which list to go create a spreadsheet of. I am saving that id into state, but that is not working because of how state works and it is always behind by a click.
The function that runs on the click is this one:
And the useQuery with the refetch:
21 Replies
deep-jadeOP•14mo ago
What is the best way to handle this so that the query runs with the correct id on click?
correct-apricot•14mo ago
Why is it not a mutation?
deep-jadeOP•14mo ago
Its just getting data from the database and creating a spreadsheet with it, should that be a mutation?
correct-apricot•14mo ago
I think a useMutation has some advantages in this scenario
deep-jadeOP•14mo ago
ok, will it allow me to send the correct id when I click the button?
basically how do I call the useMutation on command passing it a given variable?
correct-apricot•14mo ago
Did you read the documentation?
deep-jadeOP•14mo ago
yes, I've looked through it, it quite overwhelming
deep-jadeOP•14mo ago
ok, did some more digging and found this: https://github.com/TanStack/query/discussions/5820#discussioncomment-6604337
GitHub
How can i useQuery when parameter change by onClick button? · TanSt...
when i have list page, one of lists has id. when i click that one, i want to get info about that id; I use useMutation and my code like this: // Mutation const { mutateAsync: getData } = useGetData...
deep-jadeOP•14mo ago
i'll try fetchQuery right in that function. Thanks!
criminal-purple•14mo ago
Would this not work?
This is a more declaritive (and intended) way to use
useQuery
^deep-jadeOP•14mo ago
oh that makes sense! Now how would I set priceListToSpreadsheetID back to null after clicking the button?
criminal-purple•14mo ago
I dont think you want to set it to null.
Think about it this way, whatever the value of
priceToSpreadsheetID
is, useQuery
will react to the new state, and give you the new data.
So if you have 3 spreadsheets, "a", "b", & "c", if you set priceToSpreadsheetID
to "a", useQuery
will automatically fetch the spreadsheet for "a". If you want to keep viewing "a", then don't touch priceToSpreadsheetID
.
But if you want to view "b", then update priceToSpreadheetID
to "b" and useQuery
will give you the spreadsheet for "b"deep-jadeOP•14mo ago
aw
The click generates an excel spreadsheet and downloads it to the browser. So in that circumstance, clicking the same link twice wouldn't do anything the second time.
criminal-purple•14mo ago
Oh so you are not just fetching existing data, but generating a new spreadsheet?
deep-jadeOP•14mo ago
yeah, it runs code on my node server to create the spreadsheet from data in the DB and then sends it to the browser
criminal-purple•14mo ago
I see
criminal-purple•14mo ago
I think you will want to use useMutation for that
https://tanstack.com/query/latest/docs/framework/react/reference/useMutation
useMutation | TanStack Query React Docs
const {
data,
criminal-purple•14mo ago
Then the
data
that is returned from useMutation
should be the spreadsheet you just created
Let me see if I can create an example
Updated it to be more concisecriminal-purple•14mo ago
This page gives a good explanation on mutations:
https://tanstack.com/query/latest/docs/framework/react/guides/mutations
Mutations | TanStack Query React Docs
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. For this purpose, TanStack Query exports a useMutation hook.
Here's an example of a mutation that adds a new todo to the server:
deep-jadeOP•14mo ago
cool, thank you!
works beautifully! Appreciate the help!
criminal-purple•14mo ago
Awesome!