How do you handle loading, error and empty states for API data in React?

MERN: connecting React and the API · Freshers and experienced

Model answer

Track the three states plus the data. Show a loading indicator while pending, a retry-able error message on failure, and an empty-state message when the list is empty. TanStack Query exposes isPending and isError, which removes much of this boilerplate.

Learn this topic

useEffect and Fetching Data from Your API: Loading, Errors and Cleanup in the course MERN Stack Full Course: MongoDB, Express, React and Node.js.

Sign in to add your own answer.

More questions