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
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
typescript
import { useEffect, useState } from "react"; interface Section { id: string;
Building a scroll-spy hook in React
custom-hooks
intersectionobserver
dom-observation
Intermediate
8 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const SWIPE_THRESHOLD = 80; const MAX_TRANSLATE = 120; export function attachSwipeToDismiss(element, onDismiss) {
Building a swipe-to-dismiss gesture in JS
touch-events
gesture-detection
dom-manipulation
Intermediate
10 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
7 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.