React.memo and useMemo help your app run faster by remembering things so it doesn't do extra work.
React.memo and useMemo in 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.
name prop changes.const Greeting = React.memo(({ name }) => {
return <Text>Hello, {name}!</Text>;
});numbers array changes.const total = useMemo(() => numbers.reduce((a, b) => a + b, 0), [numbers]);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.
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> ); }
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.
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
React.memo in React Native?Solution
Step 1: Understand React.memo's role
React.memo is a higher-order component that caches the rendered output of a component.Step 2: Identify its effect on re-rendering
It skips re-rendering if the component's props have not changed, improving performance.Final Answer:
To prevent a component from re-rendering if its props have not changed -> Option BQuick Check:
React.memo = skip re-render if props unchanged [OK]
- Confusing React.memo with useMemo
- Thinking React.memo manages state
- Believing React.memo fetches data
useMemo in a React Native functional component?Solution
Step 1: Recall useMemo syntax
useMemo takes a function returning a value and an array of dependencies.Step 2: Match correct syntax
const memoizedValue = useMemo(() => computeValue(), [dependencies]); correctly uses an arrow function and dependency array.Final Answer:
const memoizedValue = useMemo(() => computeValue(), [dependencies]); -> Option DQuick Check:
useMemo syntax = function + dependencies array [OK]
- Passing dependencies without array brackets
- Calling computeValue immediately instead of in a function
- Omitting the arrow function wrapper
MyComponent re-renders with the same count prop?const MyComponent = React.memo(({ count }) => {
console.log('Rendered');
return {count};
});
Solution
Step 1: Understand React.memo behavior on same props
React.memo skips re-render if props are unchanged, so the component renders only once.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.Final Answer:
Rendered logged once -> Option AQuick Check:
React.memo skips re-render = one log [OK]
- Assuming component renders twice
- Thinking React.memo disables all rendering
- Confusing prop changes with state changes
useMemo:const memoizedValue = useMemo(computeValue(), [dependency]);
Solution
Step 1: Check how useMemo expects its first argument
useMemo expects a function, not the result of a function call.Step 2: Identify the mistake in the code
computeValue() is called immediately; it should be wrapped in a function like () => computeValue().Final Answer:
computeValue is called immediately instead of being passed as a function -> Option AQuick Check:
useMemo needs a function, not a direct call [OK]
- Calling function instead of passing it
- Ignoring dependency array importance
- Assuming useMemo works without import
const FilteredList = React.memo(({ list, filterFunc }) => {
const filtered = useMemo(() => list.filter(filterFunc), [list, filterFunc]);
return {filtered.length};
});Solution
Step 1: Understand React.memo role
React.memo prevents re-render if props (list, filterFunc) do not change.Step 2: Understand useMemo role
useMemo caches the filtered list calculation, avoiding expensive recalculation unless dependencies change.Step 3: Combine both for best optimization
Using both together avoids unnecessary renders and expensive recalculations.Final Answer:
Use React.memo on the component and useMemo for filtered list calculation -> Option CQuick Check:
React.memo + useMemo = best performance [OK]
- Using only one optimization and missing the other
- Not including dependencies in useMemo
- Assuming useMemo alone prevents re-render
Ready to prove it?
Pass the test and this pattern moves unknown
