Prefer, in order: (1) your framework's data layer, such as Server Components with async/await, or route loaders (React Router, Next.js), which fetch before or in parallel with rendering; (2) a server-cache library (TanStack Query, SWR, RTK Query) for client-side fetching with caching, deduplication, retries, revalidation and Suspense support; (3) useEffect + fetch only for simple cases, and then always with loading/error states, cancellation or ignoring stale responses, and cleanup. Show progress with Suspense boundaries (or explicit loading UI), and handle failures with error boundaries.
// Server Component: runs on the server, ships no JS for this component
export default async function ProductPage({ params }) {
const product = await db.products.find(params.id);
return (
<>
<ProductDetails product={product} />
<Suspense fallback={<ReviewsSkeleton />}>
<Reviews productId={product.id} /> {/* async too, streams in later */}
</Suspense>
</>
);
}
There are no client waterfalls or loading-state boilerplate, and secrets stay on the server.
function Profile({ userId }) {
const { data, error, isPending } = useQuery({
queryKey: ['user', userId],
queryFn: ({ signal }) => fetch(`/api/users/${userId}`, { signal }).then(r => {
if (!r.ok) throw new Error(r.statusText);
return r.json();
}),
staleTime: 60_000,
});
if (isPending) return <Skeleton />;
if (error) return <ErrorMessage error={error} />;
return <h1>{data.name}</h1>;
}
You get caching across components, request deduplication, background refetching, retries, pagination helpers, optimistic updates, and useSuspenseQuery for Suspense-driven loading.
useEffect (understand it, use it sparingly)function Profile({ userId }) {
const [state, setState] = useState({ status: 'loading' });
useEffect(() => {
const controller = new AbortController();
setState({ status: 'loading' });
fetch(`/api/users/${userId}`, { signal: controller.signal })
.then(r => (r.ok ? r.json() : Promise.reject(new Error(r.statusText))))
.then(data => setState({ status: 'success', data }))
.catch(error => {
if (error.name !== 'AbortError') setState({ status: 'error', error });
});
return () => controller.abort(); // prevents races when userId changes and handles Strict Mode
}, [userId]);
// render by state.status
}
use() with Suspense (React 19)use(promise) suspends until the promise resolves, but the promise must be created outside render and cached (by a framework, a library, or passed down from a Server Component). Creating a new fetch promise inside render suspends forever.
Promise.all, loaders, prefetching on hover) rather than parent-fetch-then-child-fetch.startTransition to keep old content visible during navigation.useActionState, useOptimistic) or the library's mutation API, plus cache invalidation.AbortController.use, Actions and optimistic updates for mutations.Spotted a mistake or a rights concern? Report an issue