What if your app could skip boring repeated work and run lightning fast without extra effort?
Why React.memo and useMemo in React Native? - Purpose & Use Cases
Imagine you have a mobile app with many components that update often. Each time you change something, the whole screen refreshes, even parts that didn't change.
This makes the app slow and laggy, like repainting your whole room when you only want to change one picture.
Without smart tools, your app wastes time redoing work it doesn't need to do. This slows down the app and drains battery.
It's like rewriting a whole essay every time you fix one typo -- tiring and inefficient.
React.memo and useMemo help your app remember parts that didn't change. They skip redoing work for those parts, making your app faster and smoother.
It's like putting sticky notes on your work to remind you what's done, so you only focus on what really needs fixing.
function MyComponent(props) {
return <Text>{props.value}</Text>;
}
// Re-renders every time parent updatesconst MyComponent = React.memo(function MyComponent(props) {
return <Text>{props.value}</Text>;
});
// Skips re-render if props.value is sameIt enables building fast, smooth apps that feel responsive and save battery by avoiding unnecessary work.
In a chat app, React.memo prevents message bubbles from re-rendering when you type a new message, so scrolling stays smooth.
Manual re-rendering slows apps and wastes resources.
React.memo and useMemo remember unchanged parts to skip work.
This leads to faster, smoother, and more efficient apps.
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
