0
0
Reactframework~20 mins

useMemo hook in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
useMemo Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this React component using useMemo?

Consider this React component that uses useMemo to optimize a calculation. What will be displayed when the button is clicked twice?

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

function Counter() {
  const [count, setCount] = useState(0);
  const double = useMemo(() => {
    console.log('Calculating double');
    return count * 2;
  }, [count]);

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

export default Counter;
AThe console logs 'Calculating double' only once, but the double value does not update after the first click.
BThe console logs 'Calculating double' only once, and the displayed double updates correctly each click.
CThe console logs 'Calculating double' every time the button is clicked, and the double updates correctly.
DThe console never logs 'Calculating double', and the double value stays at 0.
Attempts:
2 left
💡 Hint

Think about when useMemo recalculates the value based on dependencies.

state_output
intermediate
2:00remaining
What is the value of result after this code runs?

Given this React snippet using useMemo, what will result be after the component renders?

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

function Example() {
  const [num, setNum] = useState(5);
  const result = useMemo(() => num * 3, []);
  return <div>{result}</div>;
}

export default Example;
A15
B0
CNaN
D5
Attempts:
2 left
💡 Hint

Look at the dependency array and initial state value.

📝 Syntax
advanced
2:00remaining
Which option correctly uses useMemo to memoize a function result?

Choose the correct syntax to memoize the result of computeValue() based on input.

Aconst memoized = useMemo(computeValue(input), [input]);
Bconst memoized = useMemo(computeValue, input);
Cconst memoized = useMemo(() => computeValue, [input]);
Dconst memoized = useMemo(() => computeValue(input), [input]);
Attempts:
2 left
💡 Hint

Remember that useMemo expects a function as the first argument.

🔧 Debug
advanced
2:00remaining
Why does this useMemo hook not update when count changes?

Look at this code snippet. Why does memoizedValue not update when count changes?

React
const memoizedValue = useMemo(() => count * 2, []);
ABecause the dependency array is empty, so useMemo never recalculates.
BBecause useMemo cannot depend on state variables.
CBecause the function inside useMemo is missing a return statement.
DBecause count is not defined in the component.
Attempts:
2 left
💡 Hint

Check the dependency array carefully.

🧠 Conceptual
expert
2:00remaining
What is the main benefit of using useMemo in React components?

Choose the best explanation for why developers use useMemo in React.

ATo replace useEffect for side effects that depend on state changes.
BTo prevent expensive calculations from running on every render by caching the result until dependencies change.
CTo automatically update state variables without calling setState.
DTo memoize event handler functions to avoid re-creating them on every render.
Attempts:
2 left
💡 Hint

Think about performance optimization and when calculations run.