Choose dependencies and cleanup
Let dependencies describe resource identity
A dependency list identifies the reactive inputs needed by a setup and its cleanup. On a committed change to those inputs, React cleans up the previous synchronization and establishes the next one. React compares entries individually using Object.is. An omitted list allows synchronization after every commit of that component; an empty list is appropriate when no changing reactive input is read. Neither form guarantees a single lifetime invocation. Props, state and values calculated in the component may be reactive. Stable module constants and React state setters do not require a changing dependency.
import { useEffect, useState } from 'react';function Station({ zone }) { useEffect(() => { function receive() { console.log('Notice for', zone); } window.addEventListener('notice-' + zone, receive); return () => window.removeEventListener('notice-' + zone, receive); }, []); // Incorrect: zone is missing return <p>Selected: {zone}</p>;}export default function App() { const [zone, setZone] = useState('north'); return <main><button onClick={() => setZone('south')}>South</button> <button onClick={() => window.dispatchEvent(new Event('notice-' + zone))}>Notice</button> <Station zone={zone} /></main>;}Source-review anti-pattern: after selecting south, the label can change but the listener is still attached to notice-north. The setup closure captured the north value. A valid JSX transformation does not detect this semantic bug. Do not fix it by suppressing the exhaustive-deps rule.
import { useEffect, useState } from 'react';function Station({ zone }) { useEffect(() => { function receive() { console.log('Notice for', zone); } window.addEventListener('notice-' + zone, receive); return () => window.removeEventListener('notice-' + zone, receive); }, [zone]); return <p>Selected: {zone}</p>;}export default function App() { const [zone, setZone] = useState('north'); return <main><button onClick={() => setZone('south')}>South</button> <button onClick={() => window.dispatchEvent(new Event('notice-' + zone))}>Notice</button> <Station zone={zone} /></main>;}After effects settle, Notice initially logs Notice for north. Selecting South removes the north listener using its original zone, then registers the south listener. The next Notice logs Notice for south. Cleanup uses the prior closure intentionally. During development diagnostics, extra cleanup and setup should leave exactly the intended active listener.
Avoid an object dependency that changes unnecessarily
import { useEffect, useState } from 'react';function Station({ zone }) { useEffect(() => { const options = { event: 'notice-' + zone }; function receive() { console.log(options.event); } window.addEventListener(options.event, receive); return () => window.removeEventListener(options.event, receive); }, [zone]); return <p>{zone}</p>;}export default function App() { const [zone, setZone] = useState('north'); const [note, setNote] = useState(''); return <main><label>Note <input value={note} onChange={e => setNote(e.target.value)} /></label> <button onClick={() => setZone(v => v === 'north' ? 'south' : 'north')}>Switch zone</button> <button onClick={() => window.dispatchEvent(new Event('notice-' + zone))}>Notice</button> <Station zone={zone} /></main>;}Typing a note does not change zone, so there is no dependency-driven reconnection. A fresh object built in the component body and placed in the array would compare unequal to the previous object even with identical fields. Moving that construction into setup leaves the meaningful primitive dependency. When the callback needs only the previous state to calculate a new value, a functional updater can remove the need to read that state inside setup. Fix the design first; only use memoization when it solves a real problem. Configure the official Hooks linter in the reviewed project and treat missing dependencies as a design signal.
Practice
Predict registrations and removals after mounting north, switching south, editing an unrelated note and unmounting. Ignore extra development diagnostics for the basic trace; then explain why setup-cleanup-setup must leave the same active subscription.