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;
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?
ExpensiveCalculationDemo
The expensive calculation will not run because number hasn’t changed.
number
The console will log "Running expensive calculation..." with every click.
The expensive calculation will run every time you click "Increment Count."
The component will re-render, but the cached result from useMemo will be used.
useMemo
Nothing happens because increment is unrelated to the calculation.
increment
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?
useExpensiveCalculation
useMemo(() => calculate(number), [])
The expensive calculation will run every time the component re-renders.
The expensive calculation will only run once when the component first mounts.
The result will update to reflect the new number value.
The console will log "Running expensive calculation..." every time number changes.
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?
Clicking "Increment Count" will trigger the expensive calculation unnecessarily.
The console will log "Running expensive calculation..." only when number changes.
The expensive calculation will run every time the component re-renders, even if number doesn’t change.
The app will perform better because there’s no overhead from useMemo.
Suppose a student adds a new state variable multiplier to ExpensiveCalculationDemo and updates the hook to use it:
multiplier
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?
Adding multiplier to the dependency array [number, multiplier] will fix it.
[number, multiplier]
useMemo is broken and can’t handle two dependencies.
The calculation doesn’t need to update because number is the only important value.
The dependency array is missing multiplier.
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?
price
When the computation is fast and doesn’t need optimization.
When the component re-renders often, but price stays the same.
When the list is small (e.g., 5 items instead of 100).
When price changes frequently for each item.
This content is only available to registered users.Please register at the the home page.