📖 Scenario: You are building a simple React component that shows a user's name and the current window width. You want to update the document title with the user's name and also update the width value whenever the window is resized.
🎯 Goal: Create a React functional component that uses two separate useEffect hooks: one to update the document title with the user's name, and another to listen for window resize events and update the width state.
📋 What You'll Learn
Create a state variable name with initial value 'Alice'.
Create a state variable width with initial value equal to window.innerWidth.
Use one useEffect to update document.title to `User: ${name}` whenever name changes.
Use another useEffect to add a resize event listener on window that updates width state, and clean up the listener on unmount.
Render the name and width values inside <div> elements.
💡 Why This Matters
🌍 Real World
Many React apps need to handle multiple side effects like updating the page title and responding to window size changes separately.
💼 Career
Understanding multiple useEffect hooks is essential for React developers to manage side effects cleanly and avoid bugs.
Progress0 / 4 steps
1
Set up state variables
Create a React functional component called UserInfo. Inside it, create a state variable name with initial value 'Alice' using useState. Also create a state variable width with initial value window.innerWidth using useState.
React
Hint
Use const [name, setName] = useState('Alice') and const [width, setWidth] = useState(window.innerWidth) inside the component.
2
Add effect to update document title
Add a useEffect hook inside UserInfo that updates document.title to `User: ${name}` whenever the name state changes.
React
Hint
Use useEffect with a dependency array containing name. Inside, set document.title = `User: ${name}`.
3
Add effect to handle window resize
Add another useEffect hook inside UserInfo that adds a resize event listener on window. The listener should update the width state with window.innerWidth. Also clean up the event listener when the component unmounts.
React
Hint
Inside useEffect, define handleResize function that calls setWidth(window.innerWidth). Add it as a listener to resize event on window. Return a cleanup function that removes the listener.
4
Render the component output
Inside the UserInfo component, return a <div> that contains two <p> elements. The first shows the text Name: {name} and the second shows Window width: {width}.
React
Hint
Return a <div> with two <p> elements showing Name: {name} and Window width: {width}.
Practice
(1/5)
1. What is the main benefit of using multiple useEffect hooks in a React component?
easy
A. It combines all side effects into one function for faster execution.
B. It separates different side effects for better readability and maintenance.
C. It prevents the component from rendering multiple times.
D. It automatically optimizes the component's performance.
Solution
Step 1: Understand the purpose of multiple useEffect hooks
Multiple useEffect hooks allow you to separate different side effects logically in a component.
Step 2: Recognize the benefit of separation
This separation makes the component easier to read and maintain because each effect handles a specific task.
Final Answer:
It separates different side effects for better readability and maintenance. -> Option B
Quick Check:
Multiple effects = separation and clarity [OK]
Hint: Multiple effects separate tasks for clarity and easier updates [OK]
A. The first useEffect should not have a dependency array.
B. You cannot use two useEffect hooks in one component.
C. The second useEffect has no dependency array, so it runs after every render.
D. The console.log statements are invalid inside useEffect.
Solution
Step 1: Check dependency arrays in useEffect hooks
The first useEffect has a dependency array [value], so it runs only when value changes.
Step 2: Analyze second useEffect without dependency array
The second useEffect has no dependency array, so it runs after every render, which might be unintended.
Final Answer:
The second useEffect has no dependency array, so it runs after every render. -> Option C
Quick Check:
Missing deps means effect runs every render [OK]
Hint: Always add dependency array to control effect runs [OK]
Common Mistakes:
Thinking multiple useEffects are not allowed
Ignoring missing dependency arrays
Believing console.log is invalid inside useEffect
5. You want to fetch user data when the component mounts and also update the document title whenever the user's name changes. How should you organize your useEffect hooks?
hard
A. Use two separate useEffect hooks: one with empty deps for fetch, one with [user.name] for title.
B. Use one useEffect to fetch data and update title together with both dependencies.
C. Use one useEffect with no dependencies to do both tasks on every render.
D. Use two useEffect hooks both with empty dependency arrays.
Solution
Step 1: Separate concerns with multiple useEffect hooks
Fetching data should happen once on mount, so useEffect with empty dependency array is best.
Step 2: Update document title when user.name changes
Use a separate useEffect with [user.name] dependency to update title only when name changes.
Final Answer:
Use two separate useEffect hooks: one with empty deps for fetch, one with [user.name] for title. -> Option A
Quick Check:
Separate effects by task and dependencies [OK]
Hint: Separate effects by when and why they run [OK]
Common Mistakes:
Combining unrelated tasks in one effect
Missing dependencies causing stale updates
Running fetch on every render unintentionally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.