Bird
Raised Fist0
React Nativemobile~7 mins

React.memo and useMemo in React Native

Choose your learning style10 modes available
Introduction

React.memo and useMemo help your app run faster by remembering things so it doesn't do extra work.

When you have a component that shows the same thing many times and doesn't need to change often.
When you want to avoid re-calculating a value that takes time unless something important changes.
When your app feels slow because it keeps re-rendering parts that look the same.
When you pass big objects or functions to child components and want to stop them from re-rendering unnecessarily.
Syntax
React Native
const MemoizedComponent = React.memo(Component);

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

React.memo wraps a component to remember its last output and skip re-render if props are the same.

useMemo remembers a value from a function and only recalculates it when dependencies change.

Examples
This component will only re-render if the name prop changes.
React Native
const Greeting = React.memo(({ name }) => {
  return <Text>Hello, {name}!</Text>;
});
This calculates the sum of numbers only when the numbers array changes.
React Native
const total = useMemo(() => numbers.reduce((a, b) => a + b, 0), [numbers]);
Sample App

This app shows a count and its doubled value. The CountDisplay component only re-renders when count changes because it is wrapped with React.memo. The doubled value is calculated only when count changes using useMemo. Toggling the other state does not cause CountDisplay to re-render.

React Native
import React, { useState, useMemo } from 'react';
import { View, Text, Button } from 'react-native';

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

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

  const doubled = useMemo(() => {
    console.log('Calculating doubled');
    return count * 2;
  }, [count]);

  return (
    <View style={{ padding: 20 }}>
      <CountDisplay count={count} />
      <Text>Doubled: {doubled}</Text>
      <Button title="Increase Count" onPress={() => setCount(count + 1)} />
      <Button title="Toggle Other" onPress={() => setOther(!other)} />
    </View>
  );
}
OutputSuccess
Important Notes

React.memo only works for functional components.

useMemo should be used for expensive calculations, not for every value.

Console logs help you see when components or calculations run.

Summary

React.memo remembers a component's output and skips re-render if props are unchanged.

useMemo remembers a calculated value and only recalculates when dependencies change.

Both help improve app speed by avoiding unnecessary work.

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