Passing AJAX Results As Props to Child Component
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In React, the usual pattern is simple: fetch data in a parent component, store it in state, and pass the result down to child components as props. The important part is not the prop itself, but how you handle loading, errors, and the first render before the request has finished.
Fetch in the Parent and Pass the Result Down
The parent component is usually the right place to own the AJAX request because it controls when data is loaded and how the UI should react to loading or failure.
The parent owns the data-fetching lifecycle. The child only cares about rendering the props it receives.
Keep Children Presentational When Possible
A good rule is to let the parent manage data and let the child focus on presentation. This makes the child easier to reuse and easier to test because it does not need to know anything about fetch, endpoints, or loading strategy.
You can make the child even more focused by giving it only the data it actually needs:
This reduces coupling. PostCard does not need the full API response shape if it only renders two fields.
Handle the Initial Empty State Explicitly
AJAX data is not available on the first render, so the child must be able to render safely with an empty array, null, or loading flags. This is one of the most common places where React beginners hit runtime errors.
For example, this is fragile:
If posts is still empty, the component crashes. A safer version checks the state first:
Passing data as props is easy. Passing data as props safely is what makes the UI stable.
Common Pitfalls
One common mistake is fetching in both the parent and the child. That duplicates network logic, complicates state flow, and makes it harder to know which component owns the data.
Another problem is passing data before deciding what the loading UI should look like. A child that assumes the prop is always populated will often fail on the first render or after a network error.
Developers also sometimes pass the entire response object through many nested components even though only one small field is needed. That makes prop interfaces noisy and encourages accidental prop drilling. Pass the smallest useful shape instead.
Finally, remember to guard against setting state after unmount in long or cancellable requests. The cleanup flag in the example keeps the component from updating after it has been removed.
Summary
- Fetch data in the parent, store it in state, and pass the result to children as props.
- Keep child components focused on rendering rather than networking.
- Design for loading, error, and empty states from the start.
- Pass only the fields a child actually needs instead of the entire API response.
- Clean up asynchronous effects so unmounted components do not try to update state.

