Course contentsShow
← React
Lesson 611 of 2,39616. useEffect for Data FetchingFree lesson

When useEffect Data Fetching Still Makes Sense

Rare cases: one-off fetches on user interaction, non-REST protocols, or when you control server-side caching.

When useEffect Data Fetching Still Makes Sense

You'll learn when the "old school" useEffect fetch pattern is actually the right tool, despite modern libraries being better most of the time.


The Rare Cases Where useEffect Wins

After learning about TanStack Query's superpowers, you might think useEffect data fetching is obsolete. Not quite! There are specific scenarios where reaching for useEffect is still reasonable:

1. One-Off Fetches on User Interaction

If you need to fetch data only when a user clicks a button—not on mount, not automatically—useEffect might be overkill. A simple fetch inside an event handler works fine:

function handleDownloadReport() {
  fetch('/api/report')
    .then(res => res.json())
    .then(data => saveFile(data));
}

No need for a library if you're not caching, refetching, or managing state across renders.

2. Non-REST Protocols

If you're working with WebSockets, Server-Sent Events (SSE), or GraphQL subscriptions, libraries like TanStack Query don't handle these out of the box. A useEffect with proper cleanup is your friend:

useEffect(() => {
  const ws = new WebSocket('wss://example.com');
  ws.onmessage = (event) => setData(event.data);
  
  return () => ws.close();
}, []);

3. You Control Server-Side Caching

If your backend already handles aggressive caching (e.g., HTTP cache headers, CDN), adding a client-side cache might be redundant. A simple useEffect fetch keeps your app lighter.


The Litmus Test

Ask yourself: "Am I dealing with REST-like data that I'll fetch repeatedly, needs background updates, or shows loading spinners?" If yes → use a library. If it's a one-off or non-HTTP protocol → useEffect is fine.

Key Takeaway: useEffect data fetching still makes sense for one-off user-triggered fetches, non-REST protocols like WebSockets, or when your server already handles all caching. For typical REST APIs, modern libraries are still the better default.