Search⌘ K
AI Features

Fetching Data the React Way

Understand how to fetch data the React way by placing data requests inside useEffect, managing loading and error states, and rendering UI based on state changes. This lesson guides you to build predictable, side-effect-driven data fetching patterns that update UI automatically as data loads or fails, ensuring scalable and maintainable React components.

With traditional JavaScript, we fetch data and imperatively stitch DOM nodes together. In React, we fetch once and render declaratively. The component state becomes the single source of truth, so our UI re-renders automatically when the data arrives or fails. The key is to place side effects — like data fetching — inside useEffect, manage loading/error in state, and render the appropriate UI for each state.

Core idea

A React component should be a pure mapping from state to UI. Data fetching is a side effect that happens after the component is on screen. We’ll:

  1. Initialize loading, error, and data state. ... ...