0
0
Reactframework~20 mins

Lifecycle mapping with hooks in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lifecycle Mastery with Hooks
Get all challenges correct to earn this badge!
Test your skills under time pressure!
lifecycle
intermediate
2:00remaining
What happens when a React component with useEffect([]) mounts?
Consider a React functional component that uses useEffect with an empty dependency array []. What lifecycle phase does this represent?
React
useEffect(() => {
  console.log('Effect runs');
}, []);
AThe effect runs before the component mounts.
BThe effect runs after every render including the first.
CThe effect runs only once after the component mounts.
DThe effect never runs because dependencies are empty.
Attempts:
2 left
💡 Hint
Think about when useEffect with empty dependencies triggers.
lifecycle
intermediate
2:00remaining
When does a useEffect cleanup function run?
Given this React hook code, when is the cleanup function executed?
useEffect(() => {
  console.log('Effect setup');
  return () => {
    console.log('Cleanup');
  };
}, [count]);
React
useEffect(() => {
  console.log('Effect setup');
  return () => {
    console.log('Cleanup');
  };
}, [count]);
ABefore the effect runs again and when the component unmounts.
BOnly when the component unmounts.
CImmediately after the effect runs.
DNever, because cleanup functions are optional.
Attempts:
2 left
💡 Hint
Cleanup runs before the next effect or unmount.
component_behavior
advanced
2:00remaining
What is the output sequence of console logs for this component?
Analyze the following React component and determine the order of console logs when it mounts and then updates due to count change.
React
import React, { useState, useEffect } from 'react';

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

  useEffect(() => {
    console.log('Effect run');
    return () => {
      console.log('Cleanup');
    };
  }, [count]);

  return <button onClick={() => setCount(c => c + 1)}>Count: {count}</button>;
}
AEffect run, Cleanup, Effect run
BEffect run, Effect run, Cleanup
CCleanup, Effect run, Cleanup
DEffect run only once, no cleanup
Attempts:
2 left
💡 Hint
Remember cleanup runs before the next effect.
📝 Syntax
advanced
2:00remaining
Which useEffect hook syntax causes a syntax error?
Identify the option that will cause a syntax error in React when using useEffect.
AuseEffect(() => console.log('Hi'), []);
BuseEffect(() => { return () => console.log('Cleanup'); }, [value]);
CuseEffect(() => { console.log('Hi'); }, []);
DuseEffect(() => { console.log('Hi') }, );
Attempts:
2 left
💡 Hint
Check the dependency array syntax.
🔧 Debug
expert
3:00remaining
Why does this useEffect cause an infinite loop?
Examine the code below. Why does the component keep re-rendering infinitely?
const [count, setCount] = useState(0);
useEffect(() => {
  setCount(count + 1);
}, [count]);
React
const [count, setCount] = useState(0);
useEffect(() => {
  setCount(count + 1);
}, [count]);
ABecause useEffect has no dependency array, so it runs every render.
BBecause setCount inside useEffect updates count, triggering useEffect again endlessly.
CBecause count is not initialized properly.
DBecause setCount is called outside useEffect.
Attempts:
2 left
💡 Hint
Think about what triggers useEffect and what setCount does.