Skip to main content

Fetching a list on mount

The most common Read operation: load data when a page first appears.

The component — loading, error, data

Four states handled in order:
  1. Loading — show a spinner or “Loading…” text
  2. Error — show what went wrong
  3. Empty — data loaded but the list is empty
  4. Data — render the list
This is the pattern you’ll use for every page that displays data. It never changes.

Fetching a single record

For detail pages (e.g., /users/3), fetch one record by ID:
Notice [userId] in the dependency array. If the user navigates from one profile to another, userId changes and the effect re-runs — fetching the new user automatically.

Rendering with components

Break the display into reusable components:
The smart component (UserList) handles state and fetching. The presentational component (UserCard) just displays data. This is the composition pattern from the React Essentials section.
The companion repo’s User shape is intentionally simple: id, name, and email. If you add fields later (like role), this same read pattern still works — you just render the new properties.

Search and filter

Add client-side filtering to your list:
Fetch the full list once, then filter in the browser. This is fast for small-to-medium lists (hundreds of items). For large datasets, filter on the backend with query parameters.
For small datasets (under ~500 items), client-side filtering is simpler and faster — no extra API calls on every keystroke. For large datasets, send the search term as a query parameter: GET /api/users?search=sarah.

Refreshing data

Sometimes you need to reload data — after creating, updating, or deleting:
Option 1 (update local state) is faster. Option 2 (refetch) is simpler and always correct. Start with local state updates, and switch to refetching if you run into sync issues.

What’s next?

You can create and read records. Now let’s add editing — loading existing data into a form and sending updates back to the API.

Update operation

Edit existing records with a form that sends PUT requests to your API