TanStackT
TanStack2y ago
28 replies
moderate-tomato

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:
const createXLSXFromPriceList = (id: string) => {
    console.log({id});
    setPriceListToSpreadsheetID(id);
    refetch();
};

And the useQuery with the refetch:
const {
    data: spreadsheetResult,
    refetch
}: {
    data: string | undefined;
} = useQuery({
    queryFn: getPriceListSpreadsheet,
    queryKey: ['spreadsheetResult', priceListToSpreadsheetID],
    enabled: false,
    gcTime: 1
});
Was this page helpful?