Thuta Learning
React Native
AdvancedMobile DevelopmentIntermediate

useEffect & Side Effects

React NativeLesson 1825 min

What you'll walk away with

  • Explain the React and native-platform behavior of useEffect & Side Effects
  • Test accessibility, failure, and lifecycle cases
  • Write maintainable, testable, and performant React Native

Explain it simply

useEffect & Side Effects, in plain language

The dependency array in useEffect(callback, dependencyArray) controls when the effect should re-run — [] (empty) means it runs once on mount, [value] means it runs whenever value changes, and leaving the dependency array out entirely means it runs on every render (which can be risky). The cleanup function (the return () => {...} inside useEffect) runs when the component unmounts or before the dependency changes — useful for clearing timers or canceling subscriptions.

Problem → Why → Solution

The problem — why this concept matters

Leaving out the dependency array entirely (no 2nd argument), so the effect runs on every render and can cause an infinite loop

A common mistake

Not implementing a cleanup function and leaving timers/subscriptions dangling, causing a memory leak

Core concept — understand how it works

Build useEffect & Side Effects as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

Mental model
  1. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Explain the React and native-platform behavior of useEffect & Side Effects

Core syntax or pattern

javascript
import { useState, useEffect } from 'react';

function SearchScreen() {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);

  useEffect(() => {
    if (!query) return;
    const timeoutId = setTimeout(() => {
      fetch(`https://api.example.com/search?q=${query}`)
        .then((res) => res.json())
        .then(setResults);
    }, 500); // 500ms debounce

    return () => clearTimeout(timeoutId); // cleanup — clear the old timer while the user keeps typing
  }, [query]);

  // ...
}

Build useEffect & Side Effects as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

Read the code step by step

  1. JavaScript stateimport { useState, useEffect } from 'react';

    The dependency array in useEffect(callback, dependencyArray) controls when the effect should re-run — [] (empty) means it runs once on mount, [value] means it runs whenever value changes, and leaving the dependency array out entirely means it runs on every render (which can be risky). The cleanup function (the return () => {...} inside useEffect) runs when the component unmounts or before the dependency changes — useful for clearing timers or canceling subscriptions.

  2. native component and layoutfetch(`https://api.example.com/search?q=${query}`)

    Build useEffect & Side Effects as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.

  3. Android or iOS UI}

    Test an original useEffect & Side Effects 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. JavaScript state
  2. native component and layout
  3. Android or iOS UI

Success

Explain the React and native-platform behavior of useEffect & Side Effects

Failure

Leaving out the dependency array entirely (no 2nd argument), so the effect runs on every render and can cause an infinite loop

Let's think about it this way for a second

The dependency array in useEffect(callback, dependencyArray) controls when the effect should re-run — [] (empty) means it runs once on mount, [value] means it runs whenever value changes, and leaving the dependency array out entirely means it runs on every render (which can be risky). The cleanup function (the return () => {...} inside useEffect) runs when the component unmounts or before the dependency changes — useful for clearing timers or canceling subscriptions.

Let's connect this to a real-world scenario

On a search screen, if the API call fires immediately every time the searchQuery state changes (with no debounce), a request can go out on every single keystroke — implementing a debounce with setTimeout, and calling clearTimeout inside the cleanup function, prevents unnecessary requests while the user is still typing.

Code Example

javascript
import { useState, useEffect } from 'react';

function SearchScreen() {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);

  useEffect(() => {
    if (!query) return;
    const timeoutId = setTimeout(() => {
      fetch(`https://api.example.com/search?q=${query}`)
        .then((res) => res.json())
        .then(setResults);
    }, 500); // 500ms debounce

    return () => clearTimeout(timeoutId); // cleanup — clear the old timer while the user keeps typing
  }, [query]);

  // ...
}
You should see
While the user is typing, the request doesn't fire right away — it holds off, and only after 500ms with no typing does the API call run, just once.

Try it in 5 minutes

Implement a debounce pattern (setTimeout + cleanup) yourself on a search screen.

A quick word of caution

Updating state inside useEffect and then including that same state in the dependency array can cause an infinite loop — choose your dependencies carefully so they're precise and necessary.

Real-world usage

How to apply it in a real project

Test an original useEffect & Side Effects 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 useEffect & Side Effects 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 { useState, useEffect } from 'react';

function SearchScreen() {
  const [query, setQuery] = useState('');
  const [results, setResults] = useState([]);

  useEffect(() => {
    if (!query) return;
    const timeoutId = setTimeout(() => {
      fetch(`https://api.example.com/search?q=${query}`)
        .then((res) => res.json())
        .then(setResults);
    }, 500); // 500ms debounce

    return () => clearTimeout(timeoutId); // cleanup — clear the old timer while the user keeps typing
  }, [query]);

  // ...
}

Expected: Explain the React and native-platform behavior of useEffect & Side Effects

Show hint

Test an original useEffect & Side Effects 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 useEffect & Side Effects?

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

  • JavaScript state → native component and layout → Android or iOS UI
  • Build useEffect & Side Effects as a lifecycle-aware flow covering permission, loading, empty, offline, timeout, cancellation, retry, foreground/background transitions, and stale data. Clean up subscriptions, timers, and requests.
  • Leaving out the dependency array entirely (no 2nd argument), so the effect runs on every render and can cause an infinite loop
  • Test an original useEffect & Side Effects 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.

React Native Documentation — React Native

Easy traps

  • Leaving out the dependency array entirely (no 2nd argument), so the effect runs on every render and can cause an infinite loop
  • Not implementing a cleanup function and leaving timers/subscriptions dangling, causing a memory leak
  • Assuming one simulator's happy path proves every real-device, release-build, permission, accessibility, offline, and lifecycle path.

Hands-on Exercise

Test an original useEffect & Side Effects 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: While the user is typing, the request doesn't fire right away — it holds off, and only after 500ms with no typing does the API call run, just once.

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.

useEffect & Side Effects | Thuta Learning