0
0
Reactframework~20 mins

Dependency array usage in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dependency Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when useEffect has an empty dependency array?
Consider this React component:
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>;
}
AThe effect runs after every render, logging 'Effect runs' every second, and count increments every second.
BThe effect runs only once after the first render, logging 'Effect runs' once, and count increments every second.
CThe effect never runs because the dependency array is empty, so count never increments.
DThe effect runs once but the count does not update because setCount is not called.
Attempts:
2 left
💡 Hint
Think about what an empty dependency array means for useEffect.
state_output
intermediate
2:00remaining
What is the value of count after 3 seconds?
Given this React component:
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>;
}
A0
B1
C3
DInfinite loop causing crash
Attempts:
2 left
💡 Hint
The effect runs every time count changes, scheduling a new timeout.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in useEffect dependency array?
Which of these useEffect hooks has a syntax error in the dependency array?
AuseEffect(() => { console.log('Hi'); }, [user, age]);
BuseEffect(() => { console.log('Hi'); }, [user]);
CuseEffect(() => { console.log('Hi'); }, []);
DuseEffect(() => { console.log('Hi'); }, user, age);
Attempts:
2 left
💡 Hint
Check how the dependency array is passed as the second argument.
🔧 Debug
advanced
2:00remaining
Why does this useEffect run infinitely?
Look at this React component:
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>;
}
ABecause data changes on every fetch, triggering useEffect again and again.
BBecause fetch is called without await causing a syntax error.
CBecause useEffect has no dependency array, so it runs infinitely.
DBecause setData is called with the same value, so React re-renders infinitely.
Attempts:
2 left
💡 Hint
Think about what happens when data changes and triggers the effect again.
🧠 Conceptual
expert
2: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 value changes frequently and you want to log it inside useEffect without stale data or infinite loops.
AuseEffect(() => { console.log(value); }, [value]);
BuseEffect(() => { console.log(value); });
CuseEffect(() => { console.log(value); }, []);
DuseEffect(() => { const val = value; console.log(val); }, []);
Attempts:
2 left
💡 Hint
Think about how React tracks dependencies to avoid stale data.