الانتقال إلى المحتوى
FrontendReact· 6 دقائق للقراءة

useEffect Is Not a Lifecycle Method: Think in Synchronization

An Effect keeps something outside React in sync with what's on screen. Its dependencies describe the code.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

A trading dashboard has a PriceTicker that shows a live price for the selected symbol. Pick AAPL, then switch to TSLA: the label says TSLA, but the number keeps moving with Apple's price. The Effect inside was written as "subscribe on mount", with [] and a comment that silences the linter, because the author only wanted to subscribe once. It did exactly that: once, for the first symbol it ever saw.

The common misconception

The belief: useEffect is the Hooks version of the class lifecycle methods. [] means componentDidMount, the returned function is componentWillUnmount, and the dependency array is a list you pick to decide when the code fires: "run this when X changes, but not when Y does".

Smart developers believe it because it was the migration story. Teams moved from classes to Hooks by translating one method at a time, and plenty of tutorials still teach it that way. It's also half true: with [], an Effect does run only after the first commit, and React's own docs, looking from the component's side, describe that as running on mount. But that view tells you when the code runs, not what it's for. And once you treat the array as a schedule, a linter that disagrees with you starts to look like noise.

What actually happens

An Effect keeps one outside thing in sync

React's docs describe as a way to run code after rendering so a component can synchronize with a system outside React: a network connection, a browser API, a third-party widget, a timer. Seen from the Effect itself, there is no mount and no update. It can do only two things: start synchronizing, in the setup function, and stop, in the cleanup function that setup returns. So the useful question is "how do I start syncing with this, and how do I stop?", not "what should happen on mount?".

Dependencies describe the code, not a schedule

The dependency array must list every the Effect reads: props, state, and anything computed from them during render. After each commit, React compares every dependency with its previous value using Object.is. If one changed, React runs the previous cleanup with the old values, then setup with the new ones.

You don't choose this list; the code does, and the exhaustive-deps lint rule checks it. So [] doesn't mean "once". It's a claim: this Effect reads nothing that can change between renders. When the claim is true, the Effect runs after the first commit and cleans up when the component unmounts. When it's false, the Effect keeps working with the values from the render where it last ran.

Re-synchronizing: stop the old, start the new

That's the ticker bug. Written as synchronization, the Effect declares what it reads and how to undo its work:

TSX PriceTicker.tsx
import { useEffect, useState } from "react";export function PriceTicker({ symbol }: { symbol: string }) {  const [price, setPrice] = useState<number | null>(null);  // "componentDidMount": subscribe once, never look back  useEffect(() => {    subscribeToPrice(symbol, setPrice);    // eslint-disable-next-line react-hooks/exhaustive-deps  }, []);  return <p>{symbol}: {price ?? "…"}</p>;}

Switching to TSLA now goes like this: React renders with the new symbol, commits it, calls the cleanup from the AAPL render to unsubscribe, then runs setup to subscribe to TSLA. When the ticker leaves the screen, one last cleanup stops the subscription. The wrong version never unsubscribes at all, so every ticker that ever mounted keeps a live subscription that calls setPrice.

One leftover: until TSLA's first price arrives, price still holds Apple's last number. Rendering <PriceTicker key={symbol} symbol={symbol} /> resets it, as an earlier article in this path showed.

Strict Mode's extra cycle tests the model

In development, Strict Mode runs one extra setup and cleanup before the real setup: subscribe, unsubscribe, subscribe. If your Effect is a correct synchronization, nobody can tell. If it's "on mount" code without cleanup, you'll see two subscriptions, two connections or two listeners. React's docs put it directly: the right question isn't how to run an Effect once, but how to fix it so it works after remounting. Blocking the second run with a ref only hides the missing cleanup.

Lifecycle thinkingSynchronization thinking
[] means "run on mount"[] means "this Effect reads no props or state"
The array decides when the code runsThe array lists what the code reads, and the linter checks it
Cleanup is componentWillUnmountCleanup runs before every re-sync and once more at unmount
The second run in development is a bug to suppressThe second run checks that cleanup undoes setup
One Effect per moment: "all mount logic goes here"One Effect per system you sync with

The last row matters as a component grows. Two unrelated jobs in one Effect, like a subscription and an analytics call, re-run together whenever the inputs of either one change. Give each job its own Effect with its own dependencies.

How to apply it in your project

  1. Turn on the lint and delete the suppressionsEnable react-hooks/exhaustive-deps and search for comments that disable it. Each one marks an Effect that may be reading stale values; change the code until the linter agrees.
  2. Name the system behind every EffectFor each Effect, write down which outside thing it keeps in sync: a socket, a listener, a widget, a timer. If there's no outside system, it probably shouldn't be an Effect; the next article covers what to do instead.
  3. Write cleanup that mirrors setupSubscribe and unsubscribe, connect and disconnect, setInterval and clearInterval, add and remove a listener. If you can't undo the setup, that's the bug to fix first.
  4. Keep Strict Mode onTreat doubled connections or listeners in development as missing cleanup, not as React being strange.

Key takeaways

  • An Effect synchronizes one outside system with the current render: setup starts it, cleanup stops it.
  • Dependencies list every reactive value the Effect reads; change them by changing the code, not by editing the array.
  • [] isn't "on mount" but a claim that the Effect reads nothing reactive; when that's false, it runs with stale values.
  • Strict Mode's extra setup and cleanup is a test: if a user could notice it, the cleanup is missing.

📚 المصادر

  1. Synchronizing with Effects — React
  2. Lifecycle of Reactive Effects — React
  3. Removing Effect Dependencies — React
  4. useEffect — React API Reference
  5. exhaustive-deps — eslint-plugin-react-hooks

التعليقات (0)