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 23 of the React Fundamentals: Build Modern UIs from Scratch course
ReactJune 25, 20264 min read

Introduction to Side Effects: Managing External Logic in React

Learn why side effects like API calls belong in the useEffect hook. Distinguish between rendering and side effects to build predictable React applications.

reacthooksuseEffectweb-developmentfrontendjavascript

Previously in this course, we mastered Introduction to React State: Making Your UI Interactive to keep our components dynamic. While state manages the data that drives your UI, your application eventually needs to "reach out" to the world beyond your component’s internal logic. This lesson introduces side effects, the primary mechanism for handling these external interactions.

What Are Side Effects?

In Introduction to Component-Based Architecture in React, we discussed how React components should ideally be "pure." A pure function (or component) calculates its output based solely on its inputs (props and state) without modifying anything outside its scope.

A side effect is any operation that reaches outside the component to interact with the world. Common examples include:

  • Fetching data from an API.
  • Manually changing the document title or interacting with the DOM.
  • Setting up timers or subscriptions.
  • Logging data to an external analytics service.

If you perform these actions directly inside the body of your component function, you break the contract of purity. Because React calls your component function every time it needs to re-render, any code placed directly in the function body will execute every single time the component renders. This leads to performance bottlenecks, infinite loops, and erratic UI behavior.

Distinguishing Rendering from Effects

To understand the difference, think of your component as a calculation engine:

  1. Rendering: This is the process of calculating what the UI should look like based on current props and state. This should be fast, predictable, and free of side effects.
  2. Effects: These are the "after-effects" of the render. They happen after React has updated the DOM.

By separating these, you allow React to handle the UI update efficiently while you handle the "messy" external work in a controlled environment.

The Role of useEffect

React provides the useEffect hook to encapsulate these side effects. By wrapping your logic in useEffect, you tell React: "Do not run this code during the render phase. Wait until the component has finished painting the UI to the screen."

Here is the basic syntax:

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

function MovieBrowser() {
  useEffect(() => {
    // Your side effect logic goes here
    console.log("The component has rendered or updated.");
  });

  return <div>Movie Browser</div>;
}

Worked Example: Updating the Document Title

Let’s advance our movie-browser project. Suppose we want the browser tab title to reflect the movie search query as the user types.

We shouldn't try to manipulate document.title directly inside the component body, as it would re-run on every single keystroke, causing unnecessary overhead. Instead, we use useEffect.

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

function MovieSearch({ query }) {
  // We use the state we learned in previous lessons
  const [resultsCount, setResultsCount] = useState(0);

  useEffect(() => {
    // This runs AFTER the render
    document.title = CE9178">`Searching for: ${query}`;
  });

  return (
    <div>
      <h1>Movie Browser</h1>
      <p>Results for "{query}": {resultsCount}</p>
    </div>
  );
}

By moving the title update into useEffect, we ensure that the React component remains focused on its primary job—returning JSX—while the browser title is updated as a secondary, synchronized operation.

Hands-on Exercise

In your movie-browser project, locate your main search component.

  1. Import useEffect from 'react'.
  2. Inside your component, add a useEffect block that logs the current search query to the console.
  3. Observe the console when you type in the search bar. Notice how the log appears after the render is committed to the screen.

Common Pitfalls

  • Performing Side Effects in the Render Body: Never put fetch() calls or localStorage writes directly in the function component body. It will trigger infinite loops because an API call usually updates state, which triggers a re-render, which triggers the API call again.
  • Assuming Execution Order: Beginners often expect effects to run synchronously. Remember that effects are scheduled and run after the browser has painted the screen.
  • Forgetting to Manage Dependencies: While we will cover this in the next lesson, be aware that an useEffect without a dependency array will run after every render. This is rarely what you want.

Recap

Side effects are necessary for real-world applications, but they must be isolated from the render logic to maintain performance and predictability. The useEffect hook is your tool for scheduling these operations to run after React has committed updates to the DOM. By keeping your render function pure and pushing side effects into useEffect, you ensure your app remains performant and easy to debug.

Up next: useEffect Dependencies — learn how to control exactly when your effects run so you don't trigger unnecessary API calls or DOM updates.

Previous lessonBuilding a Movie Filter ToggleNext lesson useEffect Dependencies
Back to Blog

Similar Posts

ReactJune 25, 20263 min read

Extracting Custom Hooks: A Guide to React Reusability

Learn to create custom hooks in React to abstract complex data-fetching logic. Improve your code reusability and simplify your components by building a useFetch.

Read more
ReactReactJune 25, 20263 min read

Fetching Data from an API: A Practical React Guide

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 23 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

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
ReactReactJune 25, 20263 min read

Building a Movie Filter Toggle in React: A Beginner's Guide

Master the art of UI state management by adding a filter toggle to your movie app. Learn to control list rendering using boolean state and checkbox inputs.

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

    3 min
  • 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