Performance
- PF1 MUST measure before optimizing (P4): Perf Monitor, the React Native profiler or DevTools, in a release build. NEVER judge performance in dev mode; it is much slower by design.
- PF2 NEVER render a long or unbounded list with
ScrollView+.map(). UseFlatListfor short lists and FlashList v2 for long or mixed-height ones (defaults table). - PF3 MUST give every list a
keyExtractorwith stable IDs. With FlashList v2 also:getItemTypefor mixed item kinds, nokeyprops insiderenderItem,useRecyclingStatefor item-local state, padding incontentContainerStyle. - PF4 MUST keep list props stable (
renderItem,data,extraData). React Compiler does this; with the compiler off, memoizing these is the one standing exception to P4, because FlashList’s docs require it. - PF5 NEVER keep fast-changing state (input text, timers, scroll position) high in the tree, where every change re-renders whole screens. Keep it low (ST3); read store slices with selectors.
- PF6 MUST strip
console.*from release builds (a Babel plugin or the repo’s equivalent). NEVER log inside render, scroll or animation paths. - PF7 MUST show touch feedback before heavy work: defer the work with
requestAnimationFrameor until the navigation transition ends. - PF8 MUST request images at display size from the server or CDN. NEVER load full-resolution photos into thumbnails.
- PF9 MUST protect startup: minimal work at module scope and in root providers, heavy SDKs initialized after first render when possible, and no blocking first render on non-critical requests.
- PF10 NEVER import a whole utility library for one function (all of lodash, moment). Use built-ins such as
IntlandDatefirst.
When to memoize
Memoization only pays when it lets React skip real work, and every input involved is stable. Check whether React Compiler is on first (app.json experiments or the Babel config), then use the matching column.
| Tool | Compiler ON | Compiler OFF: use only when |
|---|---|---|
useMemo |
Escape hatch only: a value used as an Effect dependency that must not change identity every render | (1) the calculation measures 1 ms or more per interaction and its inputs rarely change; (2) the value is a prop of a React.memo component or a list (PF4); (3) the value is a dependency of another hook |
useCallback |
Escape hatch only: a function used as an Effect dependency, or handed to a library that re-subscribes when the reference changes | (1) the function is a prop of a React.memo component or a list (renderItem, onEndReached); (2) it is a dependency of another hook; (3) a listener or subscription re-attaches when it changes |
React.memo |
Not needed | The component is measurably expensive (list rows, charts, maps), its parent re-renders often with the same props, and every prop is stable |
- MZ1 NEVER memoize cheap work: string building, a
map/filterover a few hundred items, values used only in the same component’s JSX, or handlers passed to non-memoized components such asPressable’sonPress. - MZ2 MUST keep every input stable, or skip the memo: one inline object, array or arrow function in the props of a
React.memocomponent undoes it. - MZ3 MUST measure before memoizing a calculation:
console.timearound it in a release-like build, and memoize only at 1 ms or more.useMemonever speeds up the first render. - MZ4 NEVER remove existing manual memoization while touching unrelated code, even with the compiler on; removing it can change behavior. Remove it only in its own change, with tests.
- MZ5 NEVER rely on
useMemofor correctness. It is a performance hint; React may recompute. Logic that must run once belongs in state, a ref or an event handler.
// Compiler OFF, all three cases justified
const Row = React.memo(TransactionRow); // list row: rendered many times, props stable
const renderItem = useCallback(({ item }) => <Row tx={item} />, []); // list prop (PF4)
const summary = useMemo(() => summarize(txs), [txs]); // measured 3 ms on 5,000 txs
// Compiler ON, the escape hatch
const options = useMemo(() => ({ roomId }), [roomId]); // Effect dependency must keep identity
useEffect(() => { const c = connect(options); return () => c.close(); }, [options]);
Animations
- AN1 MUST run animations off the JS thread. With Reanimated installed: CSS transitions and animations for state-driven changes, shared values and worklets for gesture- or scroll-driven ones. Without it: core
AnimatedwithuseNativeDriver: true, orLayoutAnimationfor fire-and-forget changes. NEVER animate withsetStateper frame orsetInterval. - AN2 MUST, on Reanimated 4, install the matching
react-native-workletsversion (check the compatibility table, A5) and listreact-native-worklets/pluginlast in the Babel config. NEVER the oldreact-native-reanimated/pluginthere. - AN3 MUST animate
transformandopacity. NEVER animatewidth,height,toporleftto move or resize something; scale withtransform. - AN4 SHOULD build gestures with
react-native-gesture-handler+ Reanimated rather thanPanResponder. - AN5 MUST respect the OS reduce-motion setting: skip or shorten non-essential animations.
- AN6 NEVER add an animation library for a single effect when
AnimatedorLayoutAnimationcan do it (V1–V8). - AN7 SHOULD keep UI transitions around 150–300 ms, and NEVER block input while an animation runs.
// Reanimated 4 CSS transition: state-driven, runs off the JS thread, no shared values needed
<Animated.View
style={{
opacity: visible ? 1 : 0,
transform: [{ scale: pressed ? 0.97 : 1 }],
transitionProperty: ['opacity', 'transform'],
transitionDuration: 200,
}}
/>