October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

React Functional Component Unmount: How to Run Cleanup

Return a cleanup function from useEffect to undo external work when a component unmounts—and understand why React may run cleanup earlier when dependencies change.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Function components do not have a componentWillUnmount method. To clean up work tied to a component, return a cleanup function from useEffect. React runs it when the component is removed—and also before it reruns that Effect because a dependency changed.

How to clean up when a function component unmounts

Put cleanup in the function returned by an Effect’s setup function. This is the usual function-component pattern for external work that needs to be undone or stopped, such as a connection or subscription.

import { useEffect } from 'react';

function ChatRoom({ roomId }) {
  useEffect(() => {
    const connection = createConnection(roomId);
    connection.connect();

    return () => {
      connection.disconnect();
    };
  }, [roomId]);

  return <h1>{roomId}</h1>;
}

Here, the Effect connects to the room named by roomId. Its returned function disconnects that connection. The dependency list includes roomId because the Effect reads it. See React’s useEffect reference and guide to synchronizing with Effects.

Cleanup can run before unmount

Cleanup is not reserved for the moment a component disappears. When a dependency changes, React first runs the previous cleanup using the values captured by that Effect, then runs the Effect again with the new values. When the component is removed, React runs the final cleanup. This is why the example disconnects the old room before connecting to a newly selected one.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Make cleanup mirror setup: disconnect what the Effect connected, unsubscribe what it subscribed to, and stop or undo other external work the setup started.

Why cleanup may seem to run unexpectedly

A dependency changed

If the component is still visible when cleanup runs, check whether an Effect dependency changed. React runs cleanup before setting up the Effect again with the new dependency values; that is not necessarily an unmount.

Strict Mode is enabled in development

React Strict Mode performs an extra setup-and-cleanup cycle in development to reveal Effects whose cleanup does not correctly undo setup. That development check does not mean the component was removed in production. Consult the StrictMode reference.

The Effect runs after every commit

If you omit the dependency array, React reruns the Effect after every commit. If you provide dependencies, one that changes on every render can also cause repeated cleanup and setup. Check the values the Effect reads and whether those values are stable when they should be.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the Effect timing that fits the work

Use useEffect for ordinary external synchronization

useEffect is the normal choice for synchronizing with external systems, such as managing a connection. React runs Effects on the client; they do not run during server rendering.

Use useLayoutEffect only when work must happen before paint

useLayoutEffect runs before the browser repaints, which can be appropriate for visual measurement or updates that must happen before the user sees the screen. It blocks painting, so use it only when that timing is necessary. React describes it in the useLayoutEffect reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When not to add an Effect

An Effect is for synchronizing with something outside React. If code only calculates values for rendering and does not synchronize with an external system, an Effect may be unnecessary. React’s guide, You Might Not Need an Effect, explains when to avoid one.

For class-component lifecycle methods and their function-component alternatives, see React’s Component reference.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.