Pie Logo
Quiz: useEffect
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
Next.js
Advanced React
Pure Components - memo
Lifecycle - useEffect
Quiz: useEffect
Exercise: useEffect
Expensive? - useMemo
DOM Interactions - useRef
Error Boundaries
Context
Leaderboards
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: useEffect
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Lifecycle - useEffect
    Quiz: useEffect
    Exercise: useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    useEffect
    by Tomas Trescak· Lifecycle - useEffect

    QUESTION 1

    In the questions below, consider the following component:

    import React, { useState, useEffect } from "react";
    
    const LifecycleDemo = () => {
      const [count, setCount] = useState(0);
    
      useEffect(() => {
        console.log("Component mounted");
        return () => {
          console.log("Component unmounted");
        };
      }, []); 
    
      useEffect(() => {
        console.log(`Count updated to ${count}`);
    
        return () => {
          console.log(`Cleanup before count changes or unmount: ${count}`);
        };
      }, [count]); 
    
      return (
        <div>
          <h1>React Lifecycle with useEffect</h1>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
        </div>
      );
    };
    
    export default LifecycleDemo;


    QUESTION 2

    You’re building a dashboard that fetches user data when the component first loads. You decide to use useEffect to handle this. Based on the LifecycleDemo code, which useEffect setup would ensure the data fetch happens only once when the component mounts?


    QUESTION 3

    In the LifecycleDemo component, you click the "Increment" button, changing count from 0 to 1. What will you see in the console based on the second useEffect (the one tracking count)?


    QUESTION 4

    Imagine you’re adding a feature to LifecycleDemo to log "Reset" when the component unmounts. Which of these modifications to the first useEffect (empty dependency array) would achieve this?


    QUESTION 5

    A student modifies LifecycleDemo by adding a useEffect to track a new state variable user like this:

    const [user, setUser] = useState("Guest");
    useEffect(() => {
      console.log(`User is: ${user}`);
    }, [count]);
    

    If count changes from 1 to 2 but user stays "Guest," what happens?


    QUESTION 6

    You’re debugging LifecycleDemo and notice that after clicking "Increment" three times (count goes 0 → 1 → 2 → 3), the console shows multiple cleanup messages. Why does the second useEffect (with [count]) run its cleanup function repeatedly?

    This content is only available to registered users.
    Please register at the the home page.

    useEffect
    by Tomas Trescak· Lifecycle - useEffect

    QUESTION 1

    In the questions below, consider the following component:

    import React, { useState, useEffect } from "react";
    
    const LifecycleDemo = () => {
      const [count, setCount] = useState(0);
    
      useEffect(() => {
        console.log("Component mounted");
        return () => {
          console.log("Component unmounted");
        };
      }, []); 
    
      useEffect(() => {
        console.log(`Count updated to ${count}`);
    
        return () => {
          console.log(`Cleanup before count changes or unmount: ${count}`);
        };
      }, [count]); 
    
      return (
        <div>
          <h1>React Lifecycle with useEffect</h1>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
        </div>
      );
    };
    
    export default LifecycleDemo;


    QUESTION 2

    You’re building a dashboard that fetches user data when the component first loads. You decide to use useEffect to handle this. Based on the LifecycleDemo code, which useEffect setup would ensure the data fetch happens only once when the component mounts?


    QUESTION 3

    In the LifecycleDemo component, you click the "Increment" button, changing count from 0 to 1. What will you see in the console based on the second useEffect (the one tracking count)?


    QUESTION 4

    Imagine you’re adding a feature to LifecycleDemo to log "Reset" when the component unmounts. Which of these modifications to the first useEffect (empty dependency array) would achieve this?


    QUESTION 5

    A student modifies LifecycleDemo by adding a useEffect to track a new state variable user like this:

    const [user, setUser] = useState("Guest");
    useEffect(() => {
      console.log(`User is: ${user}`);
    }, [count]);
    

    If count changes from 1 to 2 but user stays "Guest," what happens?


    QUESTION 6

    You’re debugging LifecycleDemo and notice that after clicking "Increment" three times (count goes 0 → 1 → 2 → 3), the console shows multiple cleanup messages. Why does the second useEffect (with [count]) run its cleanup function repeatedly?

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: useEffect
    by Tomas Trescak
    Pie Logo
    Quiz: useEffect
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Lifecycle - useEffect
    Quiz: useEffect
    Exercise: useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    Next.js
    Advanced React
    Pure Components - memo
    Lifecycle - useEffect
    Quiz: useEffect
    Exercise: useEffect
    Expensive? - useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics