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

Review of Component Lifecycle: Mastering React Internals

Master the React lifecycle to write more predictable code. Learn how to map component mounting, updating, and unmounting phases to the hooks you use daily.

reactlifecyclehooksuseeffectfrontendweb developmentjavascript

Previously in this course, we explored how to handle asynchronous data and side effects in fetching data from an API and cleanup functions in useEffect. Now that we’ve built complex features like debounced search and custom hooks, it’s time to zoom out and look at the "big picture" of how React manages a component's existence.

Understanding the component lifecycle is the difference between writing "code that works" and writing "code that is robust." When you grasp the order of operations, you stop guessing why a state update triggered a re-render or why your API call fired twice.

The Three Phases of the Lifecycle

Every component in React moves through three distinct phases. While modern functional components use hooks rather than the old class-based methods, the underlying mechanics remain the same:

  1. Mounting: The component is being born. React creates the component instance, runs your function body, and inserts the resulting DOM nodes into the browser.
  2. Updating: The component is growing or changing. This happens when props change, state is updated, or the parent component re-renders.
  3. Unmounting: The component is being destroyed. React removes the component from the DOM tree, and it's time to clean up any lingering resources.

Mapping Lifecycle Phases to Hooks

In the functional world, we don't have componentDidMount or componentWillUnmount. Instead, we use useEffect to synchronize our components with these lifecycle moments.

  • Mounting: Represented by useEffect(() => { ... }, []). The empty dependency array tells React to run this effect exactly once after the initial render.
  • Updating: Represented by useEffect(() => { ... }, [dependencies]). The effect runs whenever any value in the array changes.
  • Unmounting: Represented by the function returned inside useEffect. If your effect returns a function, React runs that function right before the component is removed from the DOM.

Worked Example: Tracking the Lifecycle

Let’s look at a simple MovieDetail component to see these phases in action. We want to log when the component mounts, when it receives a new movie ID, and when it disappears.

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

function MovieDetail({ movieId }) {
  useEffect(() => {
    // 1. Mounting phase
    console.log("MovieDetail component mounted!");

    // 3. Unmounting phase(Cleanup)
    return () => {
      console.log("MovieDetail component unmounted!");
    };
  }, []); // Empty array = mount only

  useEffect(() => {
    // 2. Updating phase
    console.log(CE9178">`MovieDetail updated for movie: ${movieId}`);
  }, [movieId]); // Runs when movieId changes

  return <div>Viewing details for movie {movieId}</div>;
}

The Order of Operations

It's common to assume that the code inside your component function runs after the DOM is updated. That's not entirely true.

  1. Render Phase: React calls your function component. This is pure JavaScript logic. React calculates what the UI should look like.
  2. Commit Phase: React applies the changes to the real DOM.
  3. Layout/Effect Phase: Once the browser has painted the screen, React runs your useEffect hooks.

This separation is why we never trigger state updates directly in the component body—that would cause an infinite loop during the Render Phase. We always delegate side effects to the Effect Phase. For a deeper dive into controlling this flow, revisit Mastering useEffect Dependencies: Control Your React Lifecycle.

Hands-on Exercise

Open your project and navigate to your MovieCard component.

  1. Add a useEffect that logs "Card rendered" to the console.
  2. Add a cleanup function to that same useEffect that logs "Card removed".
  3. Wrap the MovieCard in a conditional check (e.g., showCard && <MovieCard />).
  4. Toggle the showCard state and observe the console. Does the cleanup function run when you hide the card?

Common Pitfalls

  • Forgetting the Dependency Array: If you omit the array, your effect runs on every render, which often leads to infinite loops if you are calling setState inside the effect.
  • Assuming Synchronicity: Remember that useEffect is asynchronous. The UI is updated before the effect runs. If you try to read DOM measurements (like div.offsetWidth) inside the effect, you are safe; doing it in the render body is unreliable.
  • Not Cleaning Up: Failing to return a cleanup function for event listeners or intervals is a leading cause of memory leaks. As we discussed in Cleanup Functions in useEffect: Preventing Memory Leaks, always clean up what you create.

Recap

The React lifecycle is a sequence of events: mounting, updating, and unmounting. We use useEffect to tap into these moments. By keeping your render logic pure and moving side effects into the appropriate lifecycle hooks, you ensure your components are performant and easy to debug.

Up next: We'll dive into the nuances of state management and how to choose the right strategy for complex applications.

Previous lessonFinalizing the Movie BrowserNext lesson Review of State Management
Back to Blog

Similar Posts

ReactJune 25, 20264 min read

Review of State Management: Choosing the Right React Strategy

Master React state management by understanding when to use local state, custom hooks, or Context. Learn best practices to build maintainable, scalable UIs.

Read more
ReactJune 25, 20263 min read

Building an Interactive Search Bar: Controlled Inputs in React

Learn to build an interactive search bar in React. Master controlled inputs by synchronizing form values with local state for a reactive, responsive UI.

Part of the course

React Fundamentals: Build Modern UIs from Scratch

beginner · Lesson 36 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
Read more
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
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

    3 min
  • 35

    Finalizing the Movie Browser

    3 min
  • 36

    Review of Component Lifecycle

    4 min
  • 37

    Review of State Management

    4 min
  • 38

    Building a Modal Component

    3 min
  • 39

    Introduction to PropTypes

    3 min
  • 40

    Performance Optimization Basics

    3 min
  • 41

    Handling Browser History

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