# The useEffect Hook

> How to synchronize your React components with external systems like APIs, databases, or the browser DOM.

Subject: React | Lesson 13 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/useeffect-basics
Previous: [Lists & Keys](https://codewithanimation.in/react/lists-and-keys.md) | Next: [The Dependency Array](https://codewithanimation.in/react/dependency-array.md)

## What it is

`useEffect` is a React Hook that lets you execute side effects in your components. A 'side effect' is any code that affects something outside the scope of the function being executed.

Examples include: fetching data from an API, setting up a subscription, manually changing the DOM, or starting a timer.

## Why it matters

React components are supposed to be **pure functions**. They should only take inputs (props/state) and return JSX. If you fetch data directly in the component body, it will fetch that data *every single time* the component re-renders (which could be 60 times a second!).

`useEffect` tells React: 'Hey, render the UI first so the user sees it, and *then* run this side effect.'

## How to use it

You pass a function to `useEffect`. By default, this function runs after every render.

```tsx
useEffect(() => {
  document.title = 'Hello World';
});
```

To prevent it from running on every render, you pass a **dependency array** as the second argument. It will only re-run if one of the dependencies has changed.

## Common mistake: The infinite loop trap

The most notorious React bug is causing an infinite loop with `useEffect`.

If your effect updates state, and you forget the dependency array, here is what happens: Component renders -> Effect runs -> State updates -> Component re-renders -> Effect runs -> State updates... and your browser crashes.

## Tip: Cleanup Functions

If your effect sets up a subscription or a timer, you must clean it up, or you'll get memory leaks. You do this by returning a function from your effect.

`return () => clearInterval(timerId);`

## Try it: The Chat Connection

Toggle the Chat component on and off. Watch how the effect connects to the server after mounting, and uses the cleanup function to disconnect when unmounting.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/useeffect-basics

## Check yourself

### Question 1

When does the code inside a `useEffect` function actually execute?

- A. Before the component renders, so the data is ready.
- B. During the render phase.
- C. After the component renders and the screen is updated.

Correct answer: C

React wants to show the UI to the user as fast as possible. It renders the component, paints the screen, and then runs your effects in the background.

- Why not A: React renders first, then runs effects.
- Why not B: Render must be pure. Effects run later.

### Question 2

Why shouldn't you make an API call directly in the main body of your component?

- A. Because components must be pure functions, and the API call would run on every single re-render.
- B. Because `fetch` is not supported in React.
- C. Because you can't use `await` in a React component.

Correct answer: A

Render functions execute constantly. If you put `fetch()` there, you will spam your server every time the user types into an input field or clicks a button.

- Why not B: React fully supports all JavaScript browser APIs.
- Why not C: While true for client components, the bigger issue is that it ruins purity and causes infinite network spam.

### Question 3

How do you clean up a subscription or timer started inside a `useEffect`?

- A. Call `useEffect.cleanup()`.
- B. Return a cleanup function from inside the effect.
- C. Write a separate `useCleanup` hook.

Correct answer: B

If you return a function from your effect, React will automatically run it right before the component unmounts, or right before the effect runs again.

- Why not A: That function does not exist.
- Why not C: There is no built-in `useCleanup` hook.

## Remember this

- Use `useEffect` for side effects: network requests, manual DOM mutations, and subscriptions.
- Effects run *after* the render is committed to the screen.
- Provide a dependency array to control exactly when the effect re-runs.
- Return a cleanup function to remove event listeners, cancel timers, or close connections.
