Bird
Raised Fist0
Reactframework~20 mins

Dependency array usage in React - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does an empty dependency array [] in a useEffect hook mean?
easy
A. The effect runs only when a state changes.
B. The effect runs after every render.
C. The effect never runs.
D. The effect runs only once after the first render.

Solution

  1. Step 1: Understand the role of dependency array

    The dependency array tells React when to run the effect based on changes in listed variables.
  2. Step 2: Interpret empty array meaning

    An empty array means no variables to watch, so React runs the effect only once after the first render.
  3. Final Answer:

    The effect runs only once after the first render. -> Option D
  4. Quick Check:

    Empty array = run once [OK]
Hint: Empty array means run effect once after mount [OK]
Common Mistakes:
  • Thinking empty array runs effect every render
  • Confusing empty array with no array
  • Assuming effect never runs with empty array
2. Which of the following is the correct syntax for a useEffect hook that runs only once after the component mounts?
easy
A. useEffect(() => { console.log('Hello'); });
B. useEffect(() => { console.log('Hello'); }, []);
C. useEffect(() => { console.log('Hello'); }, [{}]);
D. useEffect(() => { console.log('Hello'); }, [() => {}]);

Solution

  1. Step 1: Identify correct useEffect syntax

    The second argument is the dependency array controlling when the effect runs.
  2. Step 2: Check which option uses empty array

    useEffect(() => { console.log('Hello'); }, []); uses an empty array, so effect runs once after mount, which is correct.
  3. Final Answer:

    useEffect(() => { console.log('Hello'); }, []); -> Option B
  4. Quick Check:

    Empty array syntax = useEffect(() => { console.log('Hello'); }, []); [OK]
Hint: Empty array in second argument runs effect once [OK]
Common Mistakes:
  • Omitting dependency array runs effect every render
  • Using non-constant values in dependency array incorrectly
  • Confusing dependency array with effect callback
3. Consider this code:
const [count, setCount] = useState(0);
useEffect(() => {
  console.log('Count is', count);
}, [count]);

setCount(1);
setCount(2);

What will be logged to the console?
medium
A. Count is 0, Count is 2
B. Count is 0 only
C. Count is 2 only
D. Count is 0, Count is 1, Count is 2

Solution

  1. Step 1: Understand effect dependency on count

    The effect runs after first render (count=0) and whenever count changes.
  2. Step 2: Analyze state updates

    Two state updates happen quickly: setCount(1) then setCount(2). React batches them, so only final count=2 triggers effect after initial render.
  3. Final Answer:

    Count is 0, Count is 2 -> Option A
  4. Quick Check:

    Effect runs on initial and final count change [OK]
Hint: Effect runs after render with latest dependency value [OK]
Common Mistakes:
  • Assuming effect runs for every intermediate state update
  • Ignoring batching of state updates
  • Thinking effect runs before first render
4. What is wrong with this useEffect usage?
const [count, setCount] = useState(0);
useEffect(() => {
  console.log(count);
}, []);
medium
A. Effect will run only once and log initial count, ignoring updates.
B. Effect will never run because dependency array is empty.
C. Effect will run on every render causing infinite loop.
D. Syntax error due to missing dependencies.

Solution

  1. Step 1: Check dependency array usage

    Empty array means effect runs once after first render only.
  2. Step 2: Understand effect behavior with state

    Effect logs count only once with initial value, ignoring later count changes.
  3. Final Answer:

    Effect will run only once and log initial count, ignoring updates. -> Option A
  4. Quick Check:

    Empty array ignores state changes [OK]
Hint: Empty array ignores state changes inside effect [OK]
Common Mistakes:
  • Thinking effect runs on every count change
  • Assuming empty array stops effect from running
  • Confusing syntax error with logical error
5. You have a useEffect that depends on two props: userId and filter. Which dependency array is correct to ensure the effect runs only when these props change?
useEffect(() => {
  fetchData(userId, filter);
}, ???);
hard
A. []
B. [fetchData]
C. [userId, filter]
D. [userId]

Solution

  1. Step 1: Identify variables used inside effect

    The effect uses userId and filter inside its callback.
  2. Step 2: List all used variables in dependency array

    To rerun effect when either userId or filter changes, both must be in the dependency array.
  3. Final Answer:

    [userId, filter] -> Option C
  4. Quick Check:

    List all used variables in dependencies [OK]
Hint: Include all used variables in dependency array [OK]
Common Mistakes:
  • Leaving dependency array empty causing stale data
  • Including functions that don't change as dependencies
  • Listing only some used variables
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard