0
0
Reactframework~20 mins

Avoiding unnecessary renders in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Render Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using React.memo?
Consider a React functional component wrapped with React.memo. What happens when its parent re-renders but the props remain the same?
React
const Child = React.memo(({ count }) => {
  console.log('Child rendered');
  return <div>{count}</div>;
});

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

  return (
    <>
      <Child count={count} />
      <button onClick={() => setOther(other + 1)}>Change Other</button>
    </>
  );
}
AChild component renders only once initially; subsequent Parent re-renders with unchanged props do not cause Child to re-render.
BChild component never renders because React.memo prevents all renders.
CChild component re-renders every time Parent re-renders, logging 'Child rendered' each time.
DChild component re-renders only when the 'other' state changes.
Attempts:
2 left
💡 Hint
React.memo skips rendering if props are shallowly equal.
state_output
intermediate
2:00remaining
What is the console output when using useCallback incorrectly?
Given the following React component, what will be logged when clicking the 'Increment' button multiple times?
React
function Counter() {
  const [count, setCount] = React.useState(0);

  const increment = React.useCallback(() => {
    setCount(count + 1);
    console.log('Count inside increment:', count);
  }, []);

  return <button onClick={increment}>Increment</button>;
}
AAlways logs 'Count inside increment: 0' regardless of clicks.
BLogs undefined because count is not initialized.
CThrows an error because count is not in dependency array.
DLogs increasing count values: 0, 1, 2, 3, ... as button is clicked.
Attempts:
2 left
💡 Hint
Think about how useCallback dependencies affect captured variables.
🔧 Debug
advanced
2:00remaining
Why does this component re-render unnecessarily?
Examine the following React component. Why does the Child component re-render every time the Parent renders, even though props seem unchanged?
React
const Child = React.memo(({ onClick }) => {
  console.log('Child rendered');
  return <button onClick={onClick}>Click me</button>;
});

function Parent() {
  const [count, setCount] = React.useState(0);

  return (
    <>
      <Child onClick={() => setCount(count + 1)} />
      <p>{count}</p>
    </>
  );
}
ABecause the Child component lacks a key prop.
BBecause count state changes, Child must re-render regardless of props.
CBecause React.memo does not work with arrow functions.
DBecause the onClick prop is a new function on every render, React.memo sees it as changed and re-renders Child.
Attempts:
2 left
💡 Hint
Functions defined inside components are recreated on every render.
📝 Syntax
advanced
2:00remaining
Which option correctly prevents unnecessary re-renders using useMemo?
You want to memoize a computed value that depends on 'items' array. Which code snippet correctly uses useMemo to avoid recomputing unless 'items' changes?
Aconst total = React.useMemo(() => items.reduce((a, b) => a + b, 0), []);
Bconst total = React.useMemo(() => items.reduce((a, b) => a + b, 0), [items]);
Cconst total = React.useMemo(items.reduce((a, b) => a + b, 0), [items]);
Dconst total = React.useMemo(() => items.reduce((a, b) => a + b, 0));
Attempts:
2 left
💡 Hint
useMemo expects a function and a dependency array.
🧠 Conceptual
expert
3:00remaining
Which technique best avoids unnecessary renders in deeply nested React components?
You have a deeply nested component tree. Which approach most effectively prevents unnecessary renders of child components when unrelated state changes in ancestors?
ALift state up to the top and pass all state as props to every child component.
BUse React Context to pass all state and update it frequently to keep children in sync.
CUse React.memo on child components and useContext selectively with useMemo or useCallback to avoid prop changes.
DAvoid using React.memo and rely on React's default reconciliation to optimize renders.
Attempts:
2 left
💡 Hint
Think about combining memoization and selective context usage.