0
0
Reactframework~20 mins

Common lifecycle use cases in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when the component mounts and updates?

Consider this React functional component using hooks:

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

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

  useEffect(() => {
    console.log('Effect runs');
  });

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}

What will be logged to the console when the component first appears and each time the button is clicked?

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

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

  useEffect(() => {
    console.log('Effect runs');
  });

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}
ALogs 'Effect runs' once when mounted only.
BLogs 'Effect runs' every time the component renders, including mount and updates.
CLogs nothing because useEffect has no dependencies.
DLogs 'Effect runs' only when the count changes.
Attempts:
2 left
💡 Hint

Think about when useEffect runs if no dependency array is provided.

state_output
intermediate
2:00remaining
What is the displayed count after clicking the button once?

Look at this React component:

import React, { useState } from 'react';

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

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Click me twice</button>
    </div>
  );
}

What will the count show after clicking the button once?

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

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

  function handleClick() {
    setCount(count + 1);
    setCount(count + 1);
  }

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleClick}>Click me twice</button>
    </div>
  );
}
ACount will be 1 after one click.
BCount will be 2 after one click.
CCount will be 0 after one click.
DCount will be 3 after one click.
Attempts:
2 left
💡 Hint

Remember that state updates may be batched and use the current state value.

🔧 Debug
advanced
2:00remaining
Why does this cleanup function not run on unmount?

Consider this React component:

import React, { useEffect } from 'react';

function Timer() {
  useEffect(() => {
    const id = setInterval(() => console.log('tick'), 1000);
    return () => clearInterval(id);
  }, []);

  return <div>Timer running</div>;
}

But when the component unmounts, 'tick' keeps logging every second. Why?

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

function Timer() {
  useEffect(() => {
    const id = setInterval(() => console.log('tick'), 1000);
    return () => clearInterval(id);
  }, []);

  return <div>Timer running</div>;
}
AThe component never unmounts, so cleanup never runs.
BThe cleanup function is not returned properly from useEffect.
CThe setInterval id is not accessible inside the cleanup function.
DThe dependency array is empty, so cleanup never runs.
Attempts:
2 left
💡 Hint

Think about when cleanup functions run in React.

📝 Syntax
advanced
2:00remaining
Which option correctly uses useEffect to fetch data only once?

Which code snippet correctly fetches data only once when the component mounts?

AuseEffect(fetchData, []);
BuseEffect(() => { fetchData(); });
CuseEffect(() => { fetchData(); }, [fetchData]);
DuseEffect(() => { fetchData(); }, []);
Attempts:
2 left
💡 Hint

Consider how dependency arrays control effect execution.

🧠 Conceptual
expert
2:00remaining
What happens if you update state inside useEffect without dependencies?

Consider this React component:

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

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

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

  return <div>Count: {count}</div>;
}

What will happen when this component renders?

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

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

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

  return <div>Count: {count}</div>;
}
AThe count will stay at 0 because useEffect does not run.
BThe count will increment once and stop.
CThe component will enter an infinite render loop and crash.
DThe component will render twice and then stop.
Attempts:
2 left
💡 Hint

Think about what happens when state updates trigger re-renders inside useEffect with no dependencies.