Skip to content
Shubham JangleResources
React

Explore topics

API Request and Props

Watch the walkthrough
Open on Instagram

Quick revision

Props carry data from a parent to a child; a callback prop lets the child request a change owned by its parent. Do not mutate a prop to communicate upward. Destructure only the values the component needs, such as Post({ title, body }).

API request flow

  1. Start loading when the user requests data.
  2. Fetch the response and check response.ok; an HTTP error does not automatically reject fetch.
  3. Parse the response and store the result in state.
  4. Show loading, error, empty, and successful states explicitly.
  5. If requests can overlap, cancel obsolete work or ignore results from older requests.

Follow the repository example

The button fetches posts, stores them, and passes each title and body into Post. In a revised implementation use key={post.id}, replace class with className, and add an error path. Keep this button-triggered fetch in its handler; an Effect is useful when a request needs to synchronize with a changing prop or selection.

Recall check

Props are inputs; state is memory. The parent can own fetched records while a child only renders one record.

References: Passing props and Using Fetch.

Supporting material