Explain it simply
Performance Optimization, in plain language
React.memo(Component) skips re-rendering a component when its props haven't changed — this prevents every child component from automatically re-rendering whenever the parent re-renders. useMemo(calculation, deps) caches the result of an expensive calculation and returns the cached value instead of recomputing it when the dependencies haven't changed. useCallback(function, deps) keeps a function reference stable — useful when passing a function as a prop to a React.memo child (if the function reference isn't stable, React.memo stops working).
Problem → Why → Solution
The problem — why this concept matters
Adding React.memo/useMemo/useCallback to every component and calculation by default (premature optimization — it just complicates the code)
A common mistake
Not setting useCallback's dependency array correctly, causing stale closure bugs
Core concept — understand how it works
Evaluate Performance Optimization on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
- device behavior
- measure and verify
- smooth accessible UI
Explain the React and native-platform behavior of Performance Optimization
Core syntax or pattern
import { memo, useCallback, useMemo } from 'react';
const ProductCard = memo(function ProductCard({ product, onPress }) {
console.log('Rendering:', product.name); // memo avoids unnecessary re-renders when props are unchanged
return <Text onPress={() => onPress(product.id)}>{product.name}</Text>;
});
function ProductList({ products }) {
// Stable function reference so ProductCard memoization can work
const handlePress = useCallback((id) => {
console.log('Pressed:', id);
}, []);
const sortedProducts = useMemo(
() => [...products].sort((a, b) => a.price - b.price),
[products],
);
return sortedProducts.map((p) => (
<ProductCard key={p.id} product={p} onPress={handlePress} />
));
}Evaluate Performance Optimization on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
Read the code step by step
- device behavior
import { memo, useCallback, useMemo } from 'react';React.memo(Component) skips re-rendering a component when its props haven't changed — this prevents every child component from automatically re-rendering whenever the parent re-renders. useMemo(calculation, deps) caches the result of an expensive calculation and returns the cached value instead of recomputing it when the dependencies haven't changed. useCallback(function, deps) keeps a function reference stable — useful when passing a function as a prop to a React.memo child (if the function reference isn't stable, React.memo stops working).
- measure and verify
}, []);Evaluate Performance Optimization on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
- smooth accessible UI
}Test an original Performance Optimization example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- device behavior
- measure and verify
- smooth accessible UI
Success
Explain the React and native-platform behavior of Performance Optimization
Failure
Adding React.memo/useMemo/useCallback to every component and calculation by default (premature optimization — it just complicates the code)
Let's think about it this way for a second
React.memo(Component) skips re-rendering a component when its props haven't changed — this prevents every child component from automatically re-rendering whenever the parent re-renders. useMemo(calculation, deps) caches the result of an expensive calculation and returns the cached value instead of recomputing it when the dependencies haven't changed. useCallback(function, deps) keeps a function reference stable — useful when passing a function as a prop to a React.memo child (if the function reference isn't stable, React.memo stops working).
Let's connect this to a real-world scenario
If a FlatList's renderItem function creates a brand-new inline function ({item} => ...) on every component render, React.memo on the child item component won't help at all — you need to keep the renderItem function stable with useCallback. Don't optimize prematurely — profile first once there's an actual problem ('the app feels sluggish'), then do targeted optimization.
Code Example
import { memo, useCallback, useMemo } from 'react';
const ProductCard = memo(function ProductCard({ product, onPress }) {
console.log('Rendering:', product.name); // memo avoids unnecessary re-renders when props are unchanged
return <Text onPress={() => onPress(product.id)}>{product.name}</Text>;
});
function ProductList({ products }) {
// Stable function reference so ProductCard memoization can work
const handlePress = useCallback((id) => {
console.log('Pressed:', id);
}, []);
const sortedProducts = useMemo(
() => [...products].sort((a, b) => a.price - b.price),
[products],
);
return sortedProducts.map((p) => (
<ProductCard key={p.id} product={p} onPress={handlePress} />
));
}In the console log, you'll see that the ProductCard component only re-renders when the products data changes (the 'Rendering: ...' log shows up less often).Try it in 5 minutes
Run a list component with a console.log both without and with React.memo, and observe the difference in re-render count.
A quick word of caution
Sprinkling performance optimization everywhere without an actual measured problem raises code complexity and can even increase bug risk — find the bottleneck first with React DevTools Profiler, then optimize.
Real-world usage
How to apply it in a real project
Test an original Performance Optimization example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Mini challenge
Build it yourself
Test an original Performance Optimization example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
import { memo, useCallback, useMemo } from 'react';
const ProductCard = memo(function ProductCard({ product, onPress }) {
console.log('Rendering:', product.name); // memo avoids unnecessary re-renders when props are unchanged
return <Text onPress={() => onPress(product.id)}>{product.name}</Text>;
});
function ProductList({ products }) {
// Stable function reference so ProductCard memoization can work
const handlePress = useCallback((id) => {
console.log('Pressed:', id);
}, []);
const sortedProducts = useMemo(
() => [...products].sort((a, b) => a.price - b.price),
[products],
);
return sortedProducts.map((p) => (
<ProductCard key={p.id} product={p} onPress={handlePress} />
));
}Expected: Explain the React and native-platform behavior of Performance Optimization
Show hint
Test an original Performance Optimization example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- device behavior → measure and verify → smooth accessible UI
- Evaluate Performance Optimization on release devices through startup, JS/UI frame rate, memory, networking, crash recovery, permissions, signing, and store requirements—not only a debug build. Recheck New Architecture and dependency compatibility on every upgrade.
- Adding React.memo/useMemo/useCallback to every component and calculation by default (premature optimization — it just complicates the code)
- Test an original Performance Optimization example on Android and iOS with small and large screens, font scaling, dark mode, keyboard and screen reader use, slow/offline networking, denied permissions, and background/foreground transitions. Use DevTools, tests, and a release build where relevant.
Performance Overview — React Native