API Request and Props
Watch the walkthrough
Open on InstagramQuick 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
- Start loading when the user requests data.
- Fetch the response and check
response.ok; an HTTP error does not automatically rejectfetch. - Parse the response and store the result in state.
- Show loading, error, empty, and successful states explicitly.
- 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.