Challenge - 5 Problems
React.memo and useMemo Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output when using React.memo?
Consider a React Native component wrapped with React.memo. What happens when its parent re-renders but the props do not change?
React Native
const Child = React.memo(({count}) => {
console.log('Child rendered');
return <Text>{count}</Text>;
});
function Parent() {
const [count, setCount] = React.useState(0);
const [other, setOther] = React.useState(0);
return (
<>
<Child count={count} />
<Button title="Update Other" onPress={() => setOther(other + 1)} />
</>
);
}Attempts:
2 left
💡 Hint
React.memo prevents re-render if props are the same.
✗ Incorrect
React.memo wraps a component and skips re-rendering if its props have not changed. Here, 'count' is the only prop. Updating 'other' state in Parent does not affect Child's props, so Child does not re-render.
❓ ui_behavior
intermediate2:00remaining
How does useMemo affect expensive calculations?
Given a component that calculates a value using useMemo, when does the calculation run?
React Native
function ExpensiveComponent({num}) {
const computed = React.useMemo(() => {
console.log('Calculating...');
return num * 2;
}, [num]);
return <Text>{computed}</Text>;
}Attempts:
2 left
💡 Hint
useMemo caches the result until dependencies change.
✗ Incorrect
useMemo runs the function only when dependencies change. Here, it recalculates only when 'num' changes, otherwise returns cached value.
❓ lifecycle
advanced2:00remaining
Why might React.memo not prevent re-rendering?
Why does a React.memo wrapped component still re-render when passed an object prop created inline in the parent?
React Native
const Child = React.memo(({data}) => {
console.log('Child rendered');
return <Text>{data.value}</Text>;
});
function Parent() {
const [count, setCount] = React.useState(0);
return (
<>
<Child data={{value: count}} />
<Button title="Increment" onPress={() => setCount(count + 1)} />
</>
);
}Attempts:
2 left
💡 Hint
Objects created inline are new every render.
✗ Incorrect
React.memo does a shallow comparison of props. Inline objects are new references each render, so React.memo thinks props changed and re-renders.
📝 Syntax
advanced2:00remaining
What is the output of this useMemo code snippet?
What will be logged and displayed when this component renders and the button is pressed twice?
React Native
function Counter() {
const [count, setCount] = React.useState(0);
const doubled = React.useMemo(() => {
console.log('Calculating doubled');
return count * 2;
}, [count]);
return (
<>
<Text>{doubled}</Text>
<Button title="Increment" onPress={() => setCount(count + 1)} />
</>
);
}Attempts:
2 left
💡 Hint
useMemo runs on initial render and when dependencies change.
✗ Incorrect
Initially runs once logging 'Calculating doubled'. Each button press increments count, triggering useMemo recalculation and logging again. After two presses, doubled is 4.
🔧 Debug
expert2:00remaining
Why does this React.memo component still re-render unexpectedly?
Identify the reason why the Child component re-renders every time even though it is wrapped with React.memo.
React Native
const Child = React.memo(({onClick}) => {
console.log('Child rendered');
return <Button title="Click" onPress={onClick} />;
});
function Parent() {
const [count, setCount] = React.useState(0);
return (
<>
<Child onClick={() => setCount(count + 1)} />
<Text>{count}</Text>
</>
);
}Attempts:
2 left
💡 Hint
Functions declared inline are new references each render.
✗ Incorrect
Inline arrow functions create new references each render. React.memo shallow compares props and sees onClick changed, causing re-render.