Bird
Raised Fist0
Reactframework~20 mins

React.memo usage - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
React.memo Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output behavior of this React.memo wrapped component?
Consider the following React component wrapped with React.memo. What will happen when the parent component re-renders but the props remain the same?
React
import React, { useState } from 'react';

const Child = React.memo(({ count }) => {
  console.log('Child rendered');
  return <div>Count: {count}</div>;
});

export default function Parent() {
  const [count, setCount] = useState(0);
  const [other, setOther] = useState(0);

  return (
    <>
      <Child count={count} />
      <button onClick={() => setOther(other + 1)}>Change Other</button>
    </>
  );
}
AThe Child component only renders once initially and does not re-render when the Parent's 'other' state changes.
BThe Child component never renders because React.memo prevents initial rendering.
CThe Child component re-renders every time the Parent re-renders, logging 'Child rendered' each time.
DThe Child component re-renders only when the Parent's 'other' state changes.
Attempts:
2 left
💡 Hint
React.memo prevents re-rendering if props do not change.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses React.memo with a custom comparison function?
You want to prevent re-rendering of a component unless the 'user.id' prop changes. Which code snippet correctly uses React.memo with a custom comparison function?
Aconst MemoComp = React.memo(Component, (prev, next) => prev.user.id > next.user.id);
Bconst MemoComp = React.memo(Component, (prev, next) => prev.user.id !== next.user.id);
Cconst MemoComp = React.memo(Component, (prev, next) => prev.user === next.user);
Dconst MemoComp = React.memo(Component, (prev, next) => prev.user.id === next.user.id);
Attempts:
2 left
💡 Hint
The comparison function should return true if props are equal to skip re-render.
❓ state_output
advanced
2:00remaining
What will be logged when clicking the button in this React.memo example?
Analyze the code below. What will be logged to the console when the button is clicked twice?
React
import React, { useState } from 'react';

const Display = React.memo(({ value }) => {
  console.log('Display rendered with value:', value);
  return <div>{value}</div>;
});

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <>
      <Display value={count} />
      <button onClick={() => setCount(count)}>Click me</button>
    </>
  );
}
A'Display rendered with value: 0' logs twice, once on initial render and once after each click.
B'Display rendered with value: 0' logs once initially and once after the first click only.
C'Display rendered with value: 0' logs only once on initial render.
DNo logs appear because the component never renders.
Attempts:
2 left
💡 Hint
React.memo skips re-render if props are shallowly equal.
🔧 Debug
advanced
2:00remaining
Why does this React.memo component still re-render on every parent update?
Look at the code below. Despite using React.memo, the Child component re-renders every time the Parent updates. Why?
React
import React, { useState } from 'react';

const Child = React.memo(({ data }) => {
  console.log('Child rendered');
  return <div>{data.text}</div>;
});

export default function Parent() {
  const [count, setCount] = useState(0);
  const data = { text: 'Hello' };

  return (
    <>
      <Child data={data} />
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </>
  );
}
ABecause React.memo does not work with objects as props.
BBecause the 'data' object is recreated on every render, causing props to be different.
CBecause the Child component is missing a key prop.
DBecause the Parent component does not use useCallback for the button handler.
Attempts:
2 left
💡 Hint
Objects are compared by reference, not by content.
🧠 Conceptual
expert
2:00remaining
Which statement best describes React.memo's behavior?
Select the most accurate description of how React.memo works in React functional components.
AReact.memo shallowly compares props and skips re-render if props are equal by reference or primitive value.
BReact.memo prevents all re-renders of a component regardless of prop changes.
CReact.memo deeply compares all nested objects in props to decide on re-rendering.
DReact.memo only works with class components to optimize rendering.
Attempts:
2 left
💡 Hint
Think about how JavaScript compares objects and primitives.

Practice

(1/5)
1.

What is the main purpose of React.memo in React?

easy
A. To manage state inside a functional component
B. To convert a functional component into a class component
C. To prevent unnecessary re-rendering of a functional component when its props have not changed
D. To fetch data from an API automatically

Solution

  1. Step 1: Understand React.memo's role

    React.memo is a higher-order component that wraps a functional component to optimize rendering.
  2. Step 2: Identify its effect on rendering

    It skips re-rendering the component if the props are the same as the previous render.
  3. Final Answer:

    To prevent unnecessary re-rendering of a functional component when its props have not changed -> Option C
  4. Quick Check:

    React.memo optimizes rendering = A [OK]
Hint: React.memo skips re-render if props unchanged [OK]
Common Mistakes:
  • Thinking React.memo manages state
  • Confusing React.memo with class components
  • Assuming React.memo fetches data
2.

Which of the following is the correct syntax to wrap a functional component MyComponent with React.memo?

easy
A. const MemoizedComponent = React.memo(MyComponent);
B. const MemoizedComponent = React.memo => MyComponent();
C. const MemoizedComponent = React.memo(MyComponent());
D. const MemoizedComponent = React.memo{MyComponent};

Solution

  1. Step 1: Recall React.memo usage syntax

    React.memo is a function that takes a component and returns a memoized version.
  2. Step 2: Check each option's syntax

    const MemoizedComponent = React.memo(MyComponent); correctly calls React.memo with the component reference without invoking it.
  3. Final Answer:

    const MemoizedComponent = React.memo(MyComponent); -> Option A
  4. Quick Check:

    React.memo(Component) syntax = D [OK]
Hint: React.memo takes component, no parentheses after component name [OK]
Common Mistakes:
  • Calling component inside React.memo (using parentheses)
  • Using arrow function syntax incorrectly
  • Using braces instead of parentheses
3.

Consider this code snippet:

const MyComponent = React.memo(({ count }) => {
console.log('Rendered');
return <div>Count: {count}</div>;
});

function App() {
const [count, setCount] = React.useState(0);
const [text, setText] = React.useState('');

return (
<>
<MyComponent count={count} />
<input value={text} onChange={e => setText(e.target.value)} />
</>
);
}

What will be logged to the console when the user types in the input box?

medium
A. 'Rendered' logs only once when the component mounts
B. 'Rendered' logs every time the input changes
C. 'Rendered' never logs
D. 'Rendered' logs only when count changes

Solution

  1. Step 1: Understand React.memo behavior with props

    MyComponent is memoized and only re-renders if its props change. It receives count as prop.
  2. Step 2: Analyze prop changes on input update

    Typing changes text state, causing App to re-render. However, the count prop remains unchanged (same primitive value), so React.memo skips re-rendering MyComponent.
  3. Step 3: Check if console logs on input change

    Since count does not change, React.memo prevents re-render, so 'Rendered' logs only once on mount.
  4. Final Answer:

    'Rendered' logs only once when the component mounts -> Option A
  5. Quick Check:

    React.memo skips re-render if props unchanged = B [OK]
Hint: React.memo skips re-render if props unchanged, so no log on input [OK]
Common Mistakes:
  • Assuming any parent state change triggers child re-render
  • Ignoring React.memo's prop comparison
  • Thinking console logs every input change
4.

Consider this usage of React.memo:

const MyComponent = React.memo(({ value }) => {
return <div>Value: {value}</div>;
}, (prevProps, nextProps) => {
return prevProps.value === nextProps.value;
});

Which of the following correctly describes this code?

medium
A. The component function should not be an arrow function
B. The custom comparison function should return false when props are equal
C. React.memo cannot accept a second argument
D. The custom comparison function returns true when props are equal, which prevents re-rendering correctly

Solution

  1. Step 1: Understand React.memo's custom comparison function

    The second argument is a function that returns true if props are equal, meaning skip re-render.
  2. Step 2: Analyze the provided function

    The function returns true when prevProps.value === nextProps.value, which is correct to skip re-render when props are equal.
  3. Step 3: Confirm the correct description

    The custom comparison function returns true when props are equal, which prevents re-rendering correctly accurately describes that it returns true when props are equal, preventing re-rendering correctly.
  4. Final Answer:

    The custom comparison function returns true when props are equal, which prevents re-rendering correctly -> Option D
  5. Quick Check:

    Custom compare returns true to skip re-render = C [OK]
Hint: Custom compare returns true to skip re-render [OK]
Common Mistakes:
  • Thinking custom compare should return false when props equal
  • Believing React.memo can't take a second argument
  • Confusing arrow function usage
5.

You have a component wrapped with React.memo that receives a prop data which is an object. You notice it re-renders every time the parent renders, even if data has the same content. How can you fix this?

hard
A. Convert data to a string before passing it as prop
B. Use a custom comparison function in React.memo to do a deep comparison of data
C. Wrap the parent component with React.memo instead
D. Use useState inside the child component to store data

Solution

  1. Step 1: Understand why re-renders happen with object props

    Objects are compared by reference, so even if content is same, new object causes re-render.
  2. Step 2: Use custom comparison function

    React.memo accepts a second argument to compare props deeply and prevent re-render if content is equal.
  3. Step 3: Evaluate other options

    Converting to string is inefficient and error-prone; wrapping parent doesn't solve child re-render; useState inside child doesn't prevent re-render.
  4. Final Answer:

    Use a custom comparison function in React.memo to do a deep comparison of data -> Option B
  5. Quick Check:

    Deep compare object props with custom function = A [OK]
Hint: Use custom compare to deep check object props [OK]
Common Mistakes:
  • Passing new object each render without memoization
  • Ignoring object reference equality
  • Trying to fix with useState inside child
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard