
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-prefetching
Advanced tools
Use this package by 3 steps to prefetch hovered links and fix fetch waterfalls to make your apps lightning fast.
You can read this article to know more.
Assume you have an app using react-router-dom
and react-query
.
import { useQuery } from "react-query";
import { BrowserRouter, Link, Route, Routes } from "react-router-dom";
export default function App() {
return (
<BrowserRouter>
<Routes>
<Link to="/a">A</Link>
<Route path="a" element={<A />} />
</Routes>
</BrowserRouter>
);
}
function A() {
const { isLoading, data } = useQuery("A", () => fetchA());
if (isLoading) return <p>Loading</p>;
return <div> {data} <B/> </div>;
}
function B() {
const { isLoading, data } = useQuery("B", () => fetchB());
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>;
}
Then your app has fetch waterfalls issue and doesn't have the prefetching feature.
npm i react-prefetching
BrowserRouter
from react-router-dom
with PrefetchRouter
from react-prefetching
Link
and NavLink
from react-router-dom
with react-prefetching
uesQuery
, if useIsPrefetch()
is true, return the child components.import { useQuery } from "react-query";
import { Route, Routes } from "react-router-dom";
import { Link, PrefetchRouter, useIsPrefetch } from "./Prefetch";
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>;
}
Then fetch waterfalls issue is totally solved and the queries will be prefetched when users hover links. That makes your frontend app look blazingly fast. No more loading spinners!
Check this codesandbox demo to play with it.
FAQs
React prefetching
The npm package react-prefetching receives a total of 0 weekly downloads. As such, react-prefetching popularity was classified as not popular.
We found that react-prefetching demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.