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

Walkthrough

Space play step click any line
Three takeaways
  1. 1Tracking user, error, and loading separately gives callers everything they need to render each state.
  2. 2An AbortController plus an effect cleanup prevents stale responses from overwriting fresh state.
  3. 3Returning the request state as a plain object makes the hook reusable across any component.

Related explainers

javascript
const ROLE_PERMISSIONS = {
  admin: ['users:read', 'users:write', 'billing:read', 'billing:write'],
  manager: ['users:read', 'billing:read'],
  member: ['users:read'],

Role-based permissions middleware in Express

authorization middleware rbac
Intermediate 9 steps
javascript
function attachThousandSeparators(input, { locale = 'en-US' } = {}) {
  const formatter = new Intl.NumberFormat(locale);
  const groupSep = formatter.format(11111).replace(/\d/g, '')[0] || ',';
  const decimalSep = formatter.format(1.1).replace(/\d/g, '')[0] || '.';

Live thousand separators without losing the caret

dom intl caret-preservation
Advanced 8 steps
javascript
import { useReducer, useEffect } from "react";
 
const initialState = { status: "idle", data: null, error: null };
 

Building a data-fetching hook in React

custom-hooks usereducer data-fetching
Intermediate 9 steps
javascript
const express = require('express');
const app = express();
 
app.get('/health', (req, res) => res.json({ status: 'ok' }));

Graceful shutdown in an Express server

graceful-shutdown signal-handling connection-tracking
Advanced 9 steps
javascript
import { useState, useEffect, useCallback } from 'react';
 
function getColumnCount(width) {
  if (width < 640) return 1;

A responsive column hook in React

custom-hooks debouncing responsive-design
Intermediate 7 steps
javascript
function initCharacterCounter(textarea, options = {}) {
  const maxLength = options.maxLength ?? 280;
  const warnThreshold = options.warnThreshold ?? 0.9;
 

A live character counter for textareas

dom closures accessibility
Intermediate 7 steps

Share this explainer

Here's the card — post it anywhere.

How a data-fetching hook works in React — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code