Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 24 of the React Fundamentals: Build Modern UIs from Scratch course
ReactReactJune 25, 20264 min read

Mastering useEffect Dependencies: Control Your React Lifecycle

Learn to master the useEffect dependency array to control exactly when your side effects run. Avoid infinite loops and optimize your React components today.

ReactuseEffectHooksFrontendJavaScript

Previously in this course, we covered the Introduction to Side Effects: Managing External Logic in React, where we established that side effects—like fetching data or manually updating the DOM—should live outside the main render logic. Today, we add the "when" to the "what" by mastering the useEffect dependency array.

In React, the useEffect hook is your primary tool for syncing your component with the outside world. However, if you don't control when that sync happens, you'll quickly run into performance bottlenecks or, worse, infinite re-render loops.

Understanding the Dependency Array

The useEffect hook takes two arguments: a callback function (the effect) and an optional dependency array. The dependency array acts as a gatekeeper; it tells React, "Only run this effect if these specific values have changed since the last render."

If you omit the array entirely, the effect runs after every single render. This is usually not what you want.

1. Running only on mount

To run an effect exactly once—when the component first appears on the screen—you provide an empty dependency array: [].

JAVASCRIPT
import { useEffect } from CE9178">'react';

function MovieLogger() {
  useEffect(() => {
    console.log("Component mounted! This runs only once.");
  }, []); // Empty array = mount only

  return <div>Check the console!</div>;
}

2. Triggering on state changes

If you want the effect to react to specific data, you list those variables inside the array. React compares the value from the previous render to the current render. If they differ, the effect fires.

In our movie-browser app, imagine we want to log the current search query every time the user types:

JAVASCRIPT
import { useState, useEffect } from CE9178">'react';

function SearchLogger({ query }) {
  useEffect(() => {
    console.log("Search query changed to:", query);
  }, [query]); // Runs whenever CE9178">'query' changes

  return <div>Searching for: {query}</div>;
}

Worked Example: Synchronizing a Document Title

Let’s advance our project. We want the browser's tab title to update whenever the user changes their search query, so they always know what they are looking for.

JSX
import { useState, useEffect } from CE9178">'react';

export default function MovieBrowser() {
  const [query, setQuery] = useState(CE9178">'');

  // Sync document title with the search state
  useEffect(() => {
    document.title = query ? CE9178">`Searching for: ${query}` : CE9178">'Movie Browser';
  }, [query]); // Dependency: re-run only when CE9178">'query' changes

  return (
    <input 
      value={query} 
      onChange={(e) => setQuery(e.target.value)} 
      placeholder="Search movies..."
    />
  );
}

In this example, the effect is "reactive." It doesn't matter how many times the MovieBrowser component re-renders for other reasons; the document.title logic only executes if query has a new value.

Hands-on Exercise

  1. Open your current movie-browser project.
  2. Inside your main component, create a useEffect that logs a message to the console saying "Effect triggered!"
  3. Add a dependency array to that effect.
  4. Add a console.log("Rendering component...") at the top level of your component function.
  5. Observe how the logs behave when you update a state variable that is not in the dependency array versus one that is.

Common Pitfalls

  • Lying to React: The biggest mistake is omitting a variable from the dependency array that you use inside the effect. React expects the array to be a complete list of dependencies. If you use a prop or state inside the effect but don't list it in the array, the effect will use "stale" (outdated) values.
  • Infinite Loops: If you update a state variable inside an effect that also depends on that same variable, you create an infinite loop. The effect runs, updates state, which triggers a re-render, which triggers the effect, and so on.
  • Overusing useEffect: Don't use useEffect for data that can be calculated during render. If you're just transforming data (like sorting a list), do it directly in the component body instead of syncing it to state via an effect. Read more on why you should stop using useEffect for data calculations to keep your components fast.

Recap

  • The dependency array controls the execution frequency of useEffect.
  • An empty array [] ensures the effect runs only once on mount.
  • Listing variables in the array ensures the effect responds only to specific changes.
  • Always include every reactive value used inside the effect to avoid stale data bugs.

Up next: We'll take these concepts further by learning how to fetch live movie data from an API and handle the asynchronous nature of network requests.

Previous lessonIntroduction to Side EffectsNext lesson Fetching Data from an API
Back to Blog

Similar Posts

ReactReactJune 24, 20263 min read

Setting Up with Vite: A Professional React Development Environment

Learn how to initialize a professional React project using Vite. We’ll cover the project directory structure and how to launch your fast development environment.

Read more
ReactNext.jsJune 22, 2026

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 24 of 49

  1. 1

    Introduction to Component-Based Architecture

    4 min
  2. 2

    Understanding the Virtual DOM

    4 min
  3. 3

    Setting Up with Vite

    3 min
4 min read

React useEffect: A Synchronization Tool, Not State Management

React useEffect is for synchronizing external systems, not state management. Learn why treating it as a lifecycle method leads to bugs and how to fix it.

Read more
ReactReactJune 25, 20263 min read

Fetching Data from an API: A Practical React Guide

Master the fetch API in React to retrieve external data. Learn to perform asynchronous requests and store JSON responses in your component state effectively.

Read more
4

Mastering JSX Syntax

4 min
  • 5

    Creating Static Components

    3 min
  • 6

    Styling Components

    3 min
  • 7

    Passing Data with Props

    3 min
  • 8

    Dynamic Movie Cards

    3 min
  • 9

    Component Composition

    3 min
  • 10

    Conditional Rendering

    3 min
  • 11

    Rendering Lists of Data

    4 min
  • 12

    The Key Prop Explained

    4 min
  • 13

    Introduction to React State

    4 min
  • 14

    Managing State with useState

    3 min
  • 15

    Building an Interactive Search Bar

    3 min
  • 16

    Handling Click Events

    4 min
  • 17

    Updating State Based on Previous State

    4 min
  • 18

    Filtering the Movie List

    3 min
  • 19

    Handling Form Submissions

    3 min
  • 20

    Controlled Components

    4 min
  • 21

    Event Bubbling and Propagation

    3 min
  • 22

    Building a Movie Filter Toggle

    3 min
  • 23

    Introduction to Side Effects

    4 min
  • 24

    useEffect Dependencies

    4 min
  • 25

    Fetching Data from an API

    3 min
  • 26

    Handling Loading States

    3 min
  • 27

    Managing Errors

    3 min
  • 28

    Cleanup Functions in useEffect

    3 min
  • 29

    Debouncing Search Input

    3 min
  • 30

    Refactoring for Clean Code

    3 min
  • 31

    Folder Structure Best Practices

    4 min
  • 32

    Extracting Custom Hooks

    3 min
  • 33

    Prop Drilling and Context API

    Coming soon
  • 34

    Polishing the UI

    Coming soon
  • 35

    Finalizing the Movie Browser

    Coming soon
  • 36

    Review of Component Lifecycle

    Coming soon
  • 37

    Review of State Management

    Coming soon
  • 38

    Building a Modal Component

    Coming soon
  • 39

    Introduction to PropTypes

    Coming soon
  • 40

    Performance Optimization Basics

    Coming soon
  • 41

    Handling Browser History

    Coming soon
  • 42

    Working with LocalStorage

    Coming soon
  • 43

    Building a Favorites List

    Coming soon
  • 44

    Handling Media in React

    Coming soon
  • 45

    Introduction to Testing

    Coming soon
  • 46

    Debugging React Apps

    Coming soon
  • 47

    Deployment Basics

    Coming soon
  • 48

    Using External Libraries

    Coming soon
  • 49

    Advanced

    Coming soon
  • View full course