0
0
Reactframework~20 mins

Multiple effects in a component in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Effects Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component with multiple useEffect hooks?

Consider this React component that uses two useEffect hooks. What will be logged to the console when the component first renders?

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

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

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

  useEffect(() => {
    console.log('Effect B: Component mounted');
  }, []);

  return (
    <button onClick={() => setCount(count + 1)}>
      Count: {count}
    </button>
  );
}
AEffect A: 0\nEffect B: Component mounted
BEffect B: Component mounted\nEffect A: 0
CEffect A: 0
DEffect B: Component mounted
Attempts:
2 left
💡 Hint

Remember that effects with empty dependency arrays run once after the first render, and effects with dependencies run after render and when dependencies change.

state_output
intermediate
2:00remaining
What is the value of count after clicking the button twice?

Given this React component with two useEffect hooks, what will be the value of count displayed after clicking the button two times?

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

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

  useEffect(() => {
    setCount(c => c + 1);
  }, []);

  useEffect(() => {
    console.log('Count changed:', count);
  }, [count]);

  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
A2
B1
C3
D0
Attempts:
2 left
💡 Hint

Think about how the initial effect increments count once, then each button click increments count by one.

lifecycle
advanced
2:00remaining
Which option correctly describes the order of effect cleanup and setup when count changes?

In this component, what is the order of console logs when count changes from 0 to 1?

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

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

  useEffect(() => {
    console.log('Effect setup:', count);
    return () => {
      console.log('Effect cleanup:', count);
    };
  }, [count]);

  return <button onClick={() => setCount(count + 1)}>Count: {count}</button>;
}
AEffect setup: 0\nEffect setup: 1
BEffect setup: 1\nEffect cleanup: 0
CEffect cleanup: 1\nEffect setup: 0
DEffect cleanup: 0\nEffect setup: 1
Attempts:
2 left
💡 Hint

Recall that React runs cleanup of the previous effect before running the new effect.

📝 Syntax
advanced
2:00remaining
Which option will cause a runtime error due to incorrect useEffect dependencies?

Which of these useEffect hooks will cause a runtime error or infinite loop?

AuseEffect(() => { setCount(count + 1); }, [count]);
BuseEffect(() => { console.log('Count:', count); }, [count]);
CuseEffect(() => { console.log('Mounted'); }, []);
DuseEffect(() => { fetchData(); }, []);
Attempts:
2 left
💡 Hint

Consider what happens if you update state inside an effect that depends on that state.

🔧 Debug
expert
3:00remaining
Why does this component not update the document title as expected?

Examine this React component. Why does the document title not update when count changes?

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

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

  useEffect(() => {
    document.title = `Count is ${count}`;
  }, []);

  return <button onClick={() => setCount(count + 1)}>Increment</button>;
}
ABecause document.title cannot be changed inside useEffect.
BBecause the effect has an empty dependency array, it runs only once and does not update on count changes.
CBecause setCount is not called correctly inside the effect.
DBecause useState is initialized with 0, so count never changes.
Attempts:
2 left
💡 Hint

Think about how dependency arrays control when effects run.