javascript
35 lines · 8 steps
How a data-fetching hook works in React
A custom React hook fetches a user by id and cancels stale requests when the id changes.
Explained by
highlit
1import { useState, useEffect } from "react";
2
3export function useUser(userId) {
4 const [user, setUser] = useState(null);
5 const [error, setError] = useState(null);
6 const [loading, setLoading] = useState(false);
7
8 useEffect(() => {
9 if (!userId) return;
10
11 const controller = new AbortController();
12
13 setLoading(true);
14 setError(null);
15
16 fetch(`/api/users/${userId}`, { signal: controller.signal })
17 .then((res) => {
18 if (!res.ok) throw new Error(`Request failed: ${res.status}`);
19 return res.json();
20 })
21 .then((data) => {
22 setUser(data);
23 setLoading(false);
24 })
25 .catch((err) => {
26 if (err.name === "AbortError") return;
27 setError(err);
28 setLoading(false);
29 });
30
31 return () => controller.abort();
32 }, [userId]);
33
34 return { user, error, loading };
35}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Tracking user, error, and loading separately gives callers everything they need to render each state.
- 2An AbortController plus an effect cleanup prevents stale responses from overwriting fresh state.
- 3Returning the request state as a plain object makes the hook reusable across any component.
Related explainers
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
javascript
import { useState, useEffect, useCallback, useRef } from "react"; export function usePersistentForm(storageKey, initialValues) { const [values, setValues] = useState(() => {
A React hook that persists form state to localStorage
custom-hooks
localstorage
debounce
Intermediate
9 steps
javascript
function groupBy(items, keySelector) { const resolveKey = typeof keySelector === 'function' ? keySelector : (item) => item[keySelector];
Building a flexible groupBy in JavaScript
higher-order-functions
reduce
data-transformation
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-data-fetching-hook-works-in-react-explained-javascript-b6b6/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.