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.
- JavaScript state
- native component and layout
- Android or iOS UI
Explain the React and native-platform behavior of useEffect & Side Effects
Core syntax or pattern
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
- JavaScript state
import { 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.
- native component and layout
fetch(`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.
- 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.
- JavaScript state
- native component and layout
- 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
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]);
// ...
}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
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
Quick quiz 2 / 3
Quick quiz 3 / 3
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