# The Dependency Array

> How to control exactly when your side effects re-run by explicitly declaring their dependencies.

Subject: React | Lesson 14 of 48 | 4 min read
Interactive lesson: https://codewithanimation.in/react/dependency-array
Previous: [The useEffect Hook](https://codewithanimation.in/react/useeffect-basics.md) | Next: [Stale Closures in Hooks](https://codewithanimation.in/react/stale-closures.md)

## What it is

The dependency array is the optional second argument to `useEffect` (and `useMemo`/`useCallback`).

It is an array containing all the reactive values (props, state, and local variables) that your effect uses inside its body.

## Why it matters

By default, an effect runs after *every single render*. This is usually bad for performance, especially if it fetches data or manipulates the DOM.

The dependency array allows you to tell React: 'Only run this effect again if one of these specific values has changed since the last render.'

## The three configurations

**1. No array:** `useEffect(() => {...})` 
Runs after *every* render.

**2. Empty array:** `useEffect(() => {...}, [])` 
Runs *only once* after the initial render (on mount).

**3. Populated array:** `useEffect(() => {...}, [x, y])` 
Runs on mount, and re-runs *only if* `x` or `y` changes.

## Common mistake: Lying to React

The most common mistake is using a state variable inside the effect, but 'forgetting' to put it in the dependency array to stop the effect from re-running too often.

This is called 'lying to React'. If you do this, your effect will 'see' the old value of that state variable forever, because it is trapped in a **stale closure** from the first render.

Never lie to React. If your effect uses a reactive value, it *must* be in the dependency array.

## Tip: Functions are reactive values too

If you declare a function inside your component body and call it inside an effect, that function must be in the dependency array! Because the function is recreated on every render, this can cause infinite loops. (You fix this by moving the function outside the component, moving it inside the effect, or using `useCallback`).

## Try it: The Three Forms

Click the buttons to trigger a component re-render. Watch how the three different forms of `useEffect` react to the changing state.

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

## Check yourself

### Question 1

What happens if you pass an empty array `[]` as the second argument to `useEffect`?

- A. The effect runs after every render.
- B. The effect runs only once, after the component first mounts.
- C. The effect never runs.

Correct answer: B

An empty array means 'this effect depends on nothing'. Since its dependencies never change (it has none), it never re-runs after the initial mount.

- Why not A: That happens when you omit the array entirely.
- Why not C: It will always run at least once (on mount).

### Question 2

You use a state variable `count` inside your effect, but pass an empty dependency array `[]`. What will happen?

- A. React will automatically add `count` to the array for you.
- B. The effect will crash the application.
- C. The effect will only ever 'see' the initial value of `count`, even when `count` updates.

Correct answer: C

Because the effect never re-runs, it is permanently trapped in the scope (closure) of the first render, where `count` was its initial value.

- Why not A: React does not modify your code. A linter plugin might warn you, though.
- Why not B: It won't crash, but it will behave incorrectly.

### Question 3

When should you completely omit the dependency array (e.g., `useEffect(() => { ... })`)?

- A. Almost never. It means the effect runs after every single render.
- B. When you want the effect to run exactly once.
- C. When the effect doesn't use any state variables.

Correct answer: A

Omitting the array forces the effect to run on every single render. You usually want an empty array `[]` or an array with dependencies.

- Why not B: Use an empty array `[]` for that.
- Why not C: If it doesn't use state, you should still pass `[]` to prevent it from re-running unnecessarily.

## Remember this

- No array: runs after every render.
- Empty array `[]`: runs once on mount.
- Array with values `[x, y]`: runs when `x` or `y` changes.
- If a value is used inside the effect, it *must* go in the array (don't lie to React).
