Handling runtime errors
📖 Scenario: You are building a simple React app that fetches user data from an API and displays it. Sometimes the API might fail or return an error. You want to handle these runtime errors gracefully so the app does not crash and shows a friendly message instead.
🎯 Goal: Create a React functional component that fetches user data and handles runtime errors using error boundaries and state. The app should show user names when data loads successfully, and show an error message if something goes wrong.
📋 What You'll Learn
Create a React functional component called
UserList.Use
useState to hold user data and error state.Use
useEffect to fetch data from https://jsonplaceholder.typicode.com/users.Handle fetch errors by setting an error state.
Render user names if data loads, or an error message if there is an error.
💡 Why This Matters
🌍 Real World
Handling runtime errors gracefully is important in real apps to avoid crashes and provide good user experience when things go wrong.
💼 Career
React developers must know how to manage errors and loading states to build robust, user-friendly interfaces.
Progress0 / 4 steps