TanStackT
TanStack2y ago
3 replies
wet-aqua

Only fetch once.

        const { data, refetch, isFetched } = useQuery({
                queryKey: ['todos'],
                queryFn: () => (
                        fetch("http://localhost:3000/api/feed").then((res) => res.json())
                ),
                enabled: false
        })


        const handleClick = () => {
                if (!isFetched) {
                        refetch()
                }
        }


So I have a button that's across my entire Next.js site, I want to know if it's possible to make that when It's clicked the first time data will fetch but the second time it won't fetch and will use the info from the previous fetch. My above solution works but only on the current page. When I switch a page and click the button, it refetches.
Was this page helpful?