Bird
Raised Fist0
React Nativemobile~20 mins

React.memo and useMemo in React Native - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
React.memo and useMemo Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2: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)} />
    </>
  );
}
AChild component re-renders every time Parent re-renders, regardless of prop changes.
BChild component re-renders only when 'other' changes.
CChild component never re-renders after first render, even if 'count' changes.
DChild component re-renders only when 'count' changes; pressing 'Update Other' does NOT re-render Child.
Attempts:
2 left
💡 Hint
React.memo prevents re-render if props are the same.
❓ ui_behavior
intermediate
2: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>;
}
ACalculation runs only when 'num' changes; otherwise cached value is used.
BCalculation runs on every render regardless of 'num'.
CCalculation runs only once when component mounts and never again.
DCalculation never runs because useMemo caches nothing.
Attempts:
2 left
💡 Hint
useMemo caches the result until dependencies change.
❓ lifecycle
advanced
2: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)} />
    </>
  );
}
ABecause the object prop is a new reference each render, React.memo sees it as changed and re-renders Child.
BReact.memo only works with primitive props, not objects.
CReact.memo is broken and does not prevent any re-renders.
DChild re-renders because 'count' is a state variable.
Attempts:
2 left
💡 Hint
Objects created inline are new every render.
📝 Syntax
advanced
2: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)} />
    </>
  );
}
ALogs nothing; displays 0 then 0 after two presses.
BLogs 'Calculating doubled' once; displays 0 then 4 after two presses.
CLogs 'Calculating doubled' three times; displays 2 then 4 after two presses.
DLogs 'Calculating doubled' three times; displays 1 then 2 after two presses.
Attempts:
2 left
💡 Hint
useMemo runs on initial render and when dependencies change.
🔧 Debug
expert
2: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>
    </>
  );
}
AChild re-renders because setCount changes on every render.
BBecause the onClick function is recreated inline on every render, React.memo sees it as a new prop and re-renders Child.
CReact.memo does not work with function props at all.
DChild re-renders because count is passed as a prop.
Attempts:
2 left
💡 Hint
Functions declared inline are new references each render.

Practice

(1/5)
1. What is the main purpose of React.memo in React Native?
easy
A. To manage component state
B. To prevent a component from re-rendering if its props have not changed
C. To fetch data from an API
D. To memorize a value between renders

Solution

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

    React.memo is a higher-order component that caches the rendered output of a component.
  2. Step 2: Identify its effect on re-rendering

    It skips re-rendering if the component's props have not changed, improving performance.
  3. Final Answer:

    To prevent a component from re-rendering if its props have not changed -> Option B
  4. Quick Check:

    React.memo = skip re-render if props unchanged [OK]
Hint: React.memo skips re-render when props stay the same [OK]
Common Mistakes:
  • Confusing React.memo with useMemo
  • Thinking React.memo manages state
  • Believing React.memo fetches data
2. Which of the following is the correct syntax to use useMemo in a React Native functional component?
easy
A. const memoizedValue = useMemo(computeValue(), dependencies);
B. const memoizedValue = useMemo(computeValue, dependencies);
C. const memoizedValue = useMemo(() => computeValue, dependencies);
D. const memoizedValue = useMemo(() => computeValue(), [dependencies]);

Solution

  1. Step 1: Recall useMemo syntax

    useMemo takes a function returning a value and an array of dependencies.
  2. Step 2: Match correct syntax

    const memoizedValue = useMemo(() => computeValue(), [dependencies]); correctly uses an arrow function and dependency array.
  3. Final Answer:

    const memoizedValue = useMemo(() => computeValue(), [dependencies]); -> Option D
  4. Quick Check:

    useMemo syntax = function + dependencies array [OK]
Hint: useMemo needs a function and dependency array [OK]
Common Mistakes:
  • Passing dependencies without array brackets
  • Calling computeValue immediately instead of in a function
  • Omitting the arrow function wrapper
3. Given the code below, what will be logged when MyComponent re-renders with the same count prop?
const MyComponent = React.memo(({ count }) => {
  console.log('Rendered');
  return {count};
});


medium
A. Rendered logged once
B. Rendered logged twice
C. No logs
D. Error due to duplicate components

Solution

  1. Step 1: Understand React.memo behavior on same props

    React.memo skips re-render if props are unchanged, so the component renders only once.
  2. Step 2: Analyze console.log calls

    React.memo prevents re-rendering when props are the same on subsequent renders. Thus, 'Rendered' is logged only once, on the initial render.
  3. Final Answer:

    Rendered logged once -> Option A
  4. Quick Check:

    React.memo skips re-render = one log [OK]
Hint: React.memo skips re-render if props unchanged, so one log [OK]
Common Mistakes:
  • Assuming component renders twice
  • Thinking React.memo disables all rendering
  • Confusing prop changes with state changes
4. Identify the error in this React Native code using useMemo:
const memoizedValue = useMemo(computeValue(), [dependency]);
medium
A. computeValue is called immediately instead of being passed as a function
B. Dependency array should be empty
C. useMemo cannot be used in functional components
D. Missing import of useMemo

Solution

  1. Step 1: Check how useMemo expects its first argument

    useMemo expects a function, not the result of a function call.
  2. Step 2: Identify the mistake in the code

    computeValue() is called immediately; it should be wrapped in a function like () => computeValue().
  3. Final Answer:

    computeValue is called immediately instead of being passed as a function -> Option A
  4. Quick Check:

    useMemo needs a function, not a direct call [OK]
Hint: Pass function to useMemo, not function call [OK]
Common Mistakes:
  • Calling function instead of passing it
  • Ignoring dependency array importance
  • Assuming useMemo works without import
5. You have a component that receives a large list as a prop and a function to filter it. You want to optimize rendering and avoid recalculating the filtered list unnecessarily. Which combination is best?
const FilteredList = React.memo(({ list, filterFunc }) => {
  const filtered = useMemo(() => list.filter(filterFunc), [list, filterFunc]);
  return {filtered.length};
});
hard
A. Only use useMemo for filtered list, no React.memo needed
B. Only use React.memo on the component, no useMemo needed
C. Use React.memo on the component and useMemo for filtered list calculation
D. Neither React.memo nor useMemo are useful here

Solution

  1. Step 1: Understand React.memo role

    React.memo prevents re-render if props (list, filterFunc) do not change.
  2. Step 2: Understand useMemo role

    useMemo caches the filtered list calculation, avoiding expensive recalculation unless dependencies change.
  3. Step 3: Combine both for best optimization

    Using both together avoids unnecessary renders and expensive recalculations.
  4. Final Answer:

    Use React.memo on the component and useMemo for filtered list calculation -> Option C
  5. Quick Check:

    React.memo + useMemo = best performance [OK]
Hint: Combine React.memo and useMemo for props and expensive calculations [OK]
Common Mistakes:
  • Using only one optimization and missing the other
  • Not including dependencies in useMemo
  • Assuming useMemo alone prevents re-render
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