0
0
Reactframework~20 mins

What is useEffect in React - Practice Questions & Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
useEffect Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the primary purpose of useEffect in React?

In React functional components, what does the useEffect hook mainly do?

AIt replaces the useState hook to manage component state.
BIt manages side effects like data fetching, subscriptions, or manually changing the DOM after rendering.
CIt is used to create new React components dynamically.
DIt automatically optimizes component rendering performance.
Attempts:
2 left
💡 Hint

Think about tasks that happen after the component shows on screen, like loading data or setting timers.

component_behavior
intermediate
2:00remaining
When does useEffect run with an empty dependency array?

Consider this React code snippet:

useEffect(() => { console.log('Effect ran'); }, []);

When will this effect run?

React
useEffect(() => { console.log('Effect ran'); }, []);
AOnly once, right after the component mounts (first render).
BEvery time the component re-renders.
COnly when the component unmounts.
DEvery time any state or prop changes.
Attempts:
2 left
💡 Hint

Empty array means no dependencies to watch for changes.

state_output
advanced
2:00remaining
What is logged when useEffect updates state without dependencies?

What will be logged to the console when this component runs?

import React, { useState, useEffect } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(count + 1);
    console.log(count);
  });

  return <div>Count: {count}</div>;
}
React
import React, { useState, useEffect } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    setCount(count + 1);
    console.log(count);
  });

  return <div>Count: {count}</div>;
}
AThrows an error because setCount is called inside useEffect.
BLogs 0 once and stops updating count.
CLogs 1 once and stops updating count.
DLogs 0, then 1, then 2, ... infinitely causing a render loop.
Attempts:
2 left
💡 Hint

Think about what happens when state changes inside useEffect without dependencies.

🔧 Debug
advanced
2:00remaining
Why does this useEffect cleanup not work as expected?

Examine this React code:

useEffect(() => {
  const id = setInterval(() => console.log('Tick'), 1000);
  return () => clearInterval(id + 1);
}, []);

Why does the interval not clear properly when the component unmounts?

React
useEffect(() => {
  const id = setInterval(() => console.log('Tick'), 1000);
  return () => clearInterval(id + 1);
}, []);
ABecause setInterval is not allowed inside useEffect.
BBecause the cleanup function is missing the dependency array.
CBecause clearInterval is called with id + 1, which is not the correct interval ID.
DBecause the interval ID is not stored in a ref.
Attempts:
2 left
💡 Hint

Check the argument passed to clearInterval.

lifecycle
expert
3:00remaining
In what order do these useEffect hooks run and clean up?

Given this component:

function Example() {
  useEffect(() => {
    console.log('Effect 1');
    return () => console.log('Cleanup 1');
  }, []);

  useEffect(() => {
    console.log('Effect 2');
    return () => console.log('Cleanup 2');
  });

  return <div>Example</div>;
}

What is the correct sequence of console logs when the component mounts and then unmounts?

React
function Example() {
  useEffect(() => {
    console.log('Effect 1');
    return () => console.log('Cleanup 1');
  }, []);

  useEffect(() => {
    console.log('Effect 2');
    return () => console.log('Cleanup 2');
  });

  return <div>Example</div>;
}
AMount: Effect 1, Effect 2; Unmount: Cleanup 2, Cleanup 1
BMount: Effect 2, Effect 1; Unmount: Cleanup 1, Cleanup 2
CMount: Effect 1, Effect 2; Unmount: Cleanup 1, Cleanup 2
DMount: Effect 2, Effect 1; Unmount: Cleanup 2, Cleanup 1
Attempts:
2 left
💡 Hint

Remember that effects run in order, but cleanups run in reverse order.