Skip to content

Instantly share code, notes, and snippets.

@oney
Last active July 3, 2022 10:22
Show Gist options
  • Save oney/ba9df5036a6f529050227d1097b6354d to your computer and use it in GitHub Desktop.
Save oney/ba9df5036a6f529050227d1097b6354d to your computer and use it in GitHub Desktop.
import { useQuery } from "react-query";
import { Route, Routes } from "react-router-dom";
import { Link, PrefetchRouter, useIsPrefetch } from "react-prefetching";
export default function App() {
return (
<PrefetchRouter> // <- 1. replace BrowserRouter
<Routes>
<Link to="/a">A</Link> // <- 2. use Link from prefetch
<Route path="a" element={<A />} />
</Routes>
</PrefetchRouter>
);
}
function A() {
const { isLoading, data } = useQuery("A", () => fetchA());
if (useIsPrefetch()) return <B />; // <- 3. return Child if isPrefetch
if (isLoading) return <p>Loading</p>;
return <div> {data} <B /> </div>;
}
function B() {
const { isLoading, data } = useQuery("B", () => fetchB());
if (useIsPrefetch()) return <C />; // <- 3. return Child if isPrefetch
if (isLoading) return <p>Loading</p>;
return <div> {data} <C /> </div>;
}
function C() {
const { isLoading, data } = useQuery("C", () => fetchC());
if (isLoading) return <p>Loading</p>;
return <div>{data}</div>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment