Challenge - 5 Problems
Dependency Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when useEffect has an empty dependency array?
Consider this React component:
What will be the behavior of this component regarding the console logs and count updates?
import React, { useEffect, useState } from 'react';
function Timer() {
const [count, setCount] = useState(0);
useEffect(() => {
console.log('Effect runs');
const id = setInterval(() => {
setCount(c => c + 1);
}, 1000);
return () => clearInterval(id);
}, []);
return <div>Count: {count}</div>;
}What will be the behavior of this component regarding the console logs and count updates?
React
import React, { useEffect, useState } from 'react'; function Timer() { const [count, setCount] = useState(0); useEffect(() => { console.log('Effect runs'); const id = setInterval(() => { setCount(c => c + 1); }, 1000); return () => clearInterval(id); }, []); return <div>Count: {count}</div>; }
Attempts:
2 left
💡 Hint
Think about what an empty dependency array means for useEffect.
✗ Incorrect
An empty dependency array means the effect runs only once after the first render. The setInterval inside updates the count every second, so the count increments continuously. The console.log runs once.
❓ state_output
intermediate2:00remaining
What is the value of count after 3 seconds?
Given this React component:
What will be the value of
import React, { useEffect, useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const timer = setTimeout(() => {
setCount(count + 1);
}, 1000);
return () => clearTimeout(timer);
}, [count]);
return <div>Count: {count}</div>;
}What will be the value of
count after 3 seconds?React
import React, { useEffect, useState } from 'react'; function Counter() { const [count, setCount] = useState(0); useEffect(() => { const timer = setTimeout(() => { setCount(count + 1); }, 1000); return () => clearTimeout(timer); }, [count]); return <div>Count: {count}</div>; }
Attempts:
2 left
💡 Hint
The effect runs every time count changes, scheduling a new timeout.
✗ Incorrect
The effect runs after every count update, scheduling a timeout to increment count after 1 second. After 3 seconds, count increments 3 times to 3.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in useEffect dependency array?
Which of these useEffect hooks has a syntax error in the dependency array?
Attempts:
2 left
💡 Hint
Check how the dependency array is passed as the second argument.
✗ Incorrect
The dependency array must be a single array argument. Option D passes two separate arguments after the function, causing a syntax error.
🔧 Debug
advanced2:00remaining
Why does this useEffect run infinitely?
Look at this React component:
Why does the useEffect run infinitely?
import React, { useEffect, useState } from 'react';
function Example() {
const [data, setData] = useState(null);
useEffect(() => {
fetch('/api/data')
.then(res => res.json())
.then(json => setData(json));
}, [data]);
return <div>{data ? 'Loaded' : 'Loading...'}</div>;
}Why does the useEffect run infinitely?
React
import React, { useEffect, useState } from 'react'; function Example() { const [data, setData] = useState(null); useEffect(() => { fetch('/api/data') .then(res => res.json()) .then(json => setData(json)); }, [data]); return <div>{data ? 'Loaded' : 'Loading...'}</div>; }
Attempts:
2 left
💡 Hint
Think about what happens when data changes and triggers the effect again.
✗ Incorrect
The effect depends on data. When data changes after fetch, useEffect runs again, fetching again, causing an infinite loop.
🧠 Conceptual
expert2:30remaining
Which dependency array usage prevents stale closures in useEffect?
You want to use a value inside useEffect that updates over time without causing infinite loops. Which dependency array usage is correct?
Assume
Assume
value changes frequently and you want to log it inside useEffect without stale data or infinite loops.Attempts:
2 left
💡 Hint
Think about how React tracks dependencies to avoid stale data.
✗ Incorrect
Including value in the dependency array ensures useEffect runs whenever value changes, preventing stale closures. Empty or missing dependencies cause stale or repeated effects.