like-gold
like-gold2mo ago

useExport making GraphQL request before triggered

We have a useExport hook we are using on a page and it appears to be fetching data from the server even when it is not triggered. I see that queryOptions are not supported with useExport, getList so how can we disable the fetch until the export is triggered?
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @geoffatsource. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The useExport hook in Refine does not support queryOptions to disable the initial fetch. The hook is designed to fetch the data as soon as it is invoked. This is because it needs to prepare the data for export and it does this by calling the getList method of your data provider as soon as the hook is invoked. If you want to control when the data is fetched, you might need to implement a custom solution outside of the useExport hook. For example, you could use a state variable to control when to invoke the useExport hook. Here is a basic example of how you might do this:
import { useExport } from "@refinedev/core";
import { useState } from "react";

export const PostList: React.FC = () => {
const [shouldExport, setShouldExport] = useState(false);
const { triggerExport } = shouldExport ? useExport<IPost>() : {};

const handleClick = () => {
setShouldExport(true);
triggerExport && triggerExport();
}

return <button onClick={handleClick}>Export Button</button>;
};
import { useExport } from "@refinedev/core";
import { useState } from "react";

export const PostList: React.FC = () => {
const [shouldExport, setShouldExport] = useState(false);
const { triggerExport } = shouldExport ? useExport<IPost>() : {};

const handleClick = () => {
setShouldExport(true);
triggerExport && triggerExport();
}

return <button onClick={handleClick}>Export Button</button>;
};
In this example, the useExport hook is only invoked when the shouldExport state variable is true. This state variable is set to true when the button is clicked, which also triggers the export. Please note that this is a workaround and might not work in all cases. It's always recommended to follow the official documentation and use the hooks as they are intended to be used. For more information about the useExport hook, you can refer to the official documentation (https://refine.dev/docs/core/hooks/utilities/use-export). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@1014305963219615754> kapa.ai is still learning and improving, please let us know how it did by reacting below