How to populate select options from an Api call in React js on page load
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Populating a dropdown from an API is a standard React task, but the reliable version involves more than calling fetch once. You need to handle loading, request failures, and component unmounting so the select stays predictable when the page renders and when users navigate quickly.
Fetch Options on Initial Render
In a function component, the usual pattern is useEffect with an empty dependency array so the request starts once when the component mounts. Store the fetched list in state, and keep separate state for loading and error conditions.
This assumes the API returns an array of objects with code and name fields. If your backend returns a different shape, normalize it before rendering instead of leaking backend details all over the component tree.
Normalize the API Response Early
A select component gets much easier to maintain when its options all use a consistent shape such as value and label. Converting the API response once lets the rendering logic stay simple.
You can then store the normalized array in state and render from that. This is especially useful when one screen uses /api/users, another uses /api/projects, and both still want the same select component.
Abort Requests During Fast Navigation
The ignore flag shown earlier is enough to prevent state updates after unmount, but AbortController is even better because it cancels the network request itself. That matters when users switch routes quickly or type into a page that re-fetches based on filters.
For a one-time page-load request, this is usually the cleanest production pattern.
Keep the Select Controlled
A dropdown tied to API data should usually be a controlled input. That means the current value lives in React state and the value prop always reflects that state. Controlled components are easier to validate and easier to reset when upstream filters change.
For example, if the user changes a region and the list of countries is refreshed, you can clear the selected country deliberately:
Without a controlled value, the browser may keep an option selected that no longer exists in the latest data set.
Handle Empty Results Explicitly
Do not treat an empty response as the same thing as a loading state. If the request succeeds but returns no options, tell the user that clearly.
This makes debugging easier too. A blank dropdown with no explanation leaves users guessing whether the page is broken, still loading, or simply has no data.
Common Pitfalls
A common mistake is trying to read the fetched array immediately after calling setOptions. State updates are asynchronous, so debug with the actual response data or with a separate useEffect that watches options.
Another problem is using array indexes as option keys. Use a stable identifier such as a code or database ID so React can reconcile option elements correctly.
Developers also often ignore failure states. If the API returns 401, 500, or invalid JSON, the select should not quietly stay empty without context.
Finally, avoid putting the fetched options array into the dependency list of the effect that loads those same options. That creates a request loop.
Summary
- Fetch dropdown data in
useEffectwhen the component mounts. - Keep separate state for options, loading, selected value, and errors.
- Normalize API responses into a stable
valueandlabelshape. - Use a controlled
selectso resets and validation behave predictably. - Cancel or ignore stale requests when the component unmounts.
Related reading
- How to POST JSON data with Python Requests?
- How to POST JSON data with Python Requests?
- How to post query parameters with Axios?
- How to POST raw whole JSON in the body of a Retrofit request?
- How to pre-populate the sms body text via an html link
- How to prevent buttons from submitting forms
- How to post request with spring boot web-client for Form data for content type application/x-www-form-urlencoded
- How to preserve HttpContext in Web API async task

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.