Pie Logo
Quiz: useMemo
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
Expensive? - useMemo
Exercise: useMemo
Quiz: 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: useMemo
    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
    Expensive? - useMemo
    Exercise: useMemo
    Quiz: useMemo
    DOM Interactions - useRef
    Error Boundaries
    Context
    Leaderboards
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    useMemo
    by Tomas Trescak· Expensive? - useMemo

    QUESTION 1

    In the questions below consider the following compoent:

    import React, { useState } from "react";
    import useExpensiveCalculation from "./useExpensiveCalculation";
    
    const ExpensiveCalculationDemo = () => {
      const [number, setNumber] = useState(1);
      const [increment, setIncrement] = useState(0);
    
      const result = useExpensiveCalculation(number);
    
      return (
        <div>
          <h1>useMemo Hook Example</h1>
          <p>Result of Expensive Calculation: {result}</p>
    
          <button onClick={() => setNumber(number + 1)}>
            Change Input (Number: {number})
          </button>
    
          {/* Increment unrelated to expensive calculation */}
          <button onClick={() => setIncrement(increment + 1)}>
            Increment Count: {increment}
          </button>
        </div>
      );
    };
    
    export default ExpensiveCalculationDemo;


    QUESTION 2

    In the ExpensiveCalculationDemo component, you click the "Increment Count" button multiple times, but the "Change Input" button has not been clicked yet. What will happen to the expensive calculation?


    QUESTION 3

    Imagine you modify the useExpensiveCalculation hook by removing the dependency array from useMemo (i.e., useMemo(() => calculate(number), [])). What would happen when you click the "Change Input" button to increase number?


    QUESTION 4

    Look at this snippet from the useExpensiveCalculation hook:

    const result = useMemo(() => calculate(number), [number]);
    

    A teammate suggests replacing useMemo with a direct function call:

    const result = calculate(number);
    

    What would be the impact of this change in the ExpensiveCalculationDemo component?


    QUESTION 5

    Suppose a student adds a new state variable multiplier to ExpensiveCalculationDemo and updates the hook to use it:

    const result = useMemo(() => calculate(number * multiplier), [number]);
    

    Later, they realize the calculation doesn’t update when multiplier changes. Why is this happening, and how could they fix it?


    QUESTION 6

    In a real-world app, you’re rendering a list of 100 items, and each item performs an expensive computation based on its price. You wrap the computation in useMemo. Under what conditions would useMemo actually improve performance?

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

    useMemo
    by Tomas Trescak· Expensive? - useMemo

    QUESTION 1

    In the questions below consider the following compoent:

    import React, { useState } from "react";
    import useExpensiveCalculation from "./useExpensiveCalculation";
    
    const ExpensiveCalculationDemo = () => {
      const [number, setNumber] = useState(1);
      const [increment, setIncrement] = useState(0);
    
      const result = useExpensiveCalculation(number);
    
      return (
        <div>
          <h1>useMemo Hook Example</h1>
          <p>Result of Expensive Calculation: {result}</p>
    
          <button onClick={() => setNumber(number + 1)}>
            Change Input (Number: {number})
          </button>
    
          {/* Increment unrelated to expensive calculation */}
          <button onClick={() => setIncrement(increment + 1)}>
            Increment Count: {increment}
          </button>
        </div>
      );
    };
    
    export default ExpensiveCalculationDemo;


    QUESTION 2

    In the ExpensiveCalculationDemo component, you click the "Increment Count" button multiple times, but the "Change Input" button has not been clicked yet. What will happen to the expensive calculation?


    QUESTION 3

    Imagine you modify the useExpensiveCalculation hook by removing the dependency array from useMemo (i.e., useMemo(() => calculate(number), [])). What would happen when you click the "Change Input" button to increase number?


    QUESTION 4

    Look at this snippet from the useExpensiveCalculation hook:

    const result = useMemo(() => calculate(number), [number]);
    

    A teammate suggests replacing useMemo with a direct function call:

    const result = calculate(number);
    

    What would be the impact of this change in the ExpensiveCalculationDemo component?


    QUESTION 5

    Suppose a student adds a new state variable multiplier to ExpensiveCalculationDemo and updates the hook to use it:

    const result = useMemo(() => calculate(number * multiplier), [number]);
    

    Later, they realize the calculation doesn’t update when multiplier changes. Why is this happening, and how could they fix it?


    QUESTION 6

    In a real-world app, you’re rendering a list of 100 items, and each item performs an expensive computation based on its price. You wrap the computation in useMemo. Under what conditions would useMemo actually improve performance?

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

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