📖 Scenario: You are building a simple React app that shows a welcome message when the component first appears on the screen.This helps you learn how React sets up components when they are added to the page, called the mounting phase.
🎯 Goal: Create a React functional component that displays a welcome message only once when it first appears.You will set up the component, add a state variable, and use the useEffect hook to run code during the mounting phase.
📋 What You'll Learn
Create a functional React component named WelcomeMessage
Use useState to create a state variable called message initialized to an empty string
Use useEffect with an empty dependency array to set message to 'Hello, welcome to the site!' when the component mounts
Render the message inside a <div> element
💡 Why This Matters
🌍 Real World
React components often need to run code when they first appear, like fetching data or setting up subscriptions. This is called the mounting phase.
💼 Career
Understanding the mounting phase is essential for React developers to manage side effects and initialize components properly.
Progress0 / 4 steps
1
Create the React component and import hooks
Create a functional React component named WelcomeMessage. Import useState and useEffect from 'react'. Inside the component, return a <div> with no content yet.
React
Hint
Start by importing the hooks and creating a simple component that returns an empty div.
2
Add a state variable called message
Inside the WelcomeMessage component, use useState to create a state variable named message initialized to an empty string ''.
React
Hint
Use const [message, setMessage] = useState('') to create the state variable.
3
Use useEffect to set the welcome message on mount
Inside the WelcomeMessage component, add a useEffect hook with an empty dependency array []. Inside it, call setMessage with the string 'Hello, welcome to the site!'.
React
Hint
Remember to pass an empty array as the second argument to useEffect so it runs only once on mount.
4
Render the message inside the <div>
Update the return statement inside WelcomeMessage to render the message state variable inside the <div> element.
React
Hint
Use curly braces inside JSX to show the message variable.
A. useEffect with setUser dependency causing repeated fetch
B. useEffect without dependencies fetching every render
C. useEffect with user dependency causing infinite fetch
D. useEffect with empty array to fetch once on mount
Solution
Step 1: Understand useEffect dependencies for fetching
Empty array [] runs effect once on mount; no dependencies cause repeated fetches.
Step 2: Analyze each option
The useEffect with an empty dependency array fetches data only once on mount. Without any dependency array, the effect runs after every render. Depending on user causes an infinite loop since updating user triggers re-renders. Depending on setUser runs only once because setUser is stable, but it is unnecessary and may cause ESLint warnings.
Final Answer:
useEffect with empty array to fetch once on mount -> Option D
Quick Check:
Empty [] means fetch once on mount [OK]
Hint: Empty array in useEffect fetches once on mount [OK]
Common Mistakes:
Leaving out dependency array causes infinite fetch
Using state as dependency causing loops
Misunderstanding stable functions as dependencies
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.