Thuta Learning
React Native
AdvancedMobile DevelopmentIntermediate

Performance Optimization

React NativeLesson 2425 min

What you'll walk away with

  • Explain the React and native-platform behavior of Performance Optimization
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

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.

Mental model
  1. device behavior
  2. measure and verify
  3. smooth accessible UI

Explain the React and native-platform behavior of Performance Optimization

Core syntax or pattern

javascript
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

  1. device behaviorimport { 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).

  2. 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.

  3. 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.

Execution flow
  1. device behavior
  2. measure and verify
  3. 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

javascript
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} />
  ));
}
You should see
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
javascript
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

What best describes the core idea of Performance Optimization?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Adding React.memo/useMemo/useCallback to every component and calculation by default (premature optimization — it just complicates the code)
  • Not setting useCallback's dependency array correctly, causing stale closure bugs
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

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.

You'll know it worked when: 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).

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Performance Optimization | Thuta Learning