Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Load User Data from an API | State and Side Effects
Introduction to React

Challenge: Load User Data from an API

Swipe to show menu

Task

Create a simple user profile that loads user data from an API function using useEffect.

The component must include:

  1. A state variable called user. Its initial value must be null.

  2. A useEffect hook.

    • Call the fetchUser function inside the effect.
    • Use .then() to receive the response.
    • Save the response into the user state using setUser.
  3. If user is null, render a paragraph (p element). Its text content must be Loading user....

  4. If user exists, render:

    • A heading (h2 element) that displays the user name.
    • A paragraph (p element) that displays the user job.

Starter Code

Open the starter project below and complete the missing parts of the component.

Starter code

Solution

You can compare your result with the final solution below.

Solution code

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 3. Chapter 4
some-alt