0
0
Reactframework~20 mins

React.memo usage - Practice Problems & Coding Challenges

Choose your learning style9 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.