Skip to main content
Every React component goes through a lifecycle: it’s created (mounted), updated, and eventually removed (unmounted). Understanding the component lifecycle helps you manage side effects, clean up resources, and optimize performance.

Lifecycle Phases

A component’s lifecycle can be divided into three main phases:
  1. Mounting: Component is created and inserted into the DOM
  2. Updating: Component re-renders due to changes in props or state
  3. Unmounting: Component is removed from the DOM

Lifecycle in Function Components

Function components use Hooks to handle lifecycle events. The primary Hook for lifecycle management is useEffect.

Mounting and Updating

Effect Dependencies

The dependency array controls when the effect runs:
Rules of Hooks: The useEffect Hook must be called at the top level of your component. Don’t call it inside conditions, loops, or nested functions. React relies on the order of Hook calls to track state correctly.

Cleanup (Unmounting)

Return a cleanup function from useEffect to run code when the component unmounts or before the effect runs again:
Common cleanup scenarios:

useLayoutEffect

useLayoutEffect is similar to useEffect but fires synchronously after all DOM mutations, before the browser paints:
When to use useLayoutEffect: Use it when you need to read layout information (like scroll position or element size) and synchronously make changes before the browser paints. For most cases, useEffect is preferred.

useInsertionEffect

useInsertionEffect fires before all DOM mutations. It’s primarily used by CSS-in-JS libraries to inject styles:
According to React’s source code in ReactHooks.js, the effect timing order is:
  1. useInsertionEffect - Before DOM mutations
  2. useLayoutEffect - After DOM mutations, before paint
  3. useEffect - After paint

Lifecycle in Class Components

Class components have explicit lifecycle methods. While function components with Hooks are now preferred, understanding class lifecycle methods is useful for maintaining existing code.

Mounting

Called when a component is created and inserted into the DOM:

Updating

Called when props or state change:
According to the React source code in ReactBaseClasses.js, forceUpdate() will not invoke shouldComponentUpdate, but it will invoke componentWillUpdate and componentDidUpdate.

Unmounting

Called when a component is removed from the DOM:

Error Boundaries

Class components can catch errors in child components using error boundaries:
Error boundaries do not catch errors in:
  • Event handlers (use try/catch instead)
  • Asynchronous code (setTimeout, promises)
  • Server-side rendering
  • Errors thrown in the error boundary itself

Function vs Class Lifecycle Comparison

Common Lifecycle Patterns

Fetching Data

Subscribing to External Data

Derived State

If a value can be calculated from props or state, don’t store it in state. Calculate it during render instead. This prevents synchronization bugs.

Next Steps

Hooks Overview

Explore all available React Hooks

Rendering

Learn how React renders and updates the DOM