Remove unnecessary effects and guard races
Decide whether an Effect is needed
Ask what resource must stay synchronized. If the result is just a calculation from current props or state, calculate it during render. If an operation is requested by a particular user interaction, put it in that interaction handler. Effects are appropriate when the resource should follow the committed component independently of which event caused the update.
import { useState } from 'react';const departures = ['North express', 'South local', 'North local'];function matchingDepartures(items, query) { return items.filter(item => item.toLowerCase().includes(query.trim().toLowerCase()));}export default function App() { const [query, setQuery] = useState(''); const visible = matchingDepartures(departures, query); return <main><label>Find departure <input value={query} onChange={e => setQuery(e.target.value)} /></label> <ul>{visible.map(item => <li key={item}>{item}</li>)}</ul> </main>;}North shows two departures, an empty query shows all three, and east shows none. No second state variable or effect is needed to maintain the filtered list. Doing so would introduce an additional update and a possibility of temporarily inconsistent data.
import { useState } from 'react';export default function App() { const [receipts, setReceipts] = useState([]); function reserve() { setReceipts(previous => [...previous, 'Seat requested']); } return <main><button onClick={reserve}>Request seat</button> <p>Requests: {receipts.length}</p></main>;}Each button activation requests one local receipt. Do not turn this into an Effect triggered by a Boolean flag: the operation belongs to the event, not to the presence of the component. This is a local state-only example; a real reservation would require server authorization, error handling and an appropriate duplicate-request policy.
Prevent an old result from replacing a newer selection
import { useEffect, useState } from 'react';function createQueue() { const pending = []; return { request(zone, deliver) { pending.push({ zone, deliver }); }, resolve(zone) { const index = pending.findIndex(item => item.zone === zone); if (index < 0) return false; const [item] = pending.splice(index, 1); item.deliver(zone + ' timetable'); return true; } };}function subscribeResult(queue, zone, accept) { let active = true; queue.request(zone, result => { if (active) accept(result); }); return () => { active = false; };}const queue = createQueue();function Timetable({ zone }) { const [result, setResult] = useState(null); useEffect(() => subscribeResult(queue, zone, text => setResult({ zone, text })), [zone]); return <p role="status">{result?.zone === zone ? result.text : 'Waiting for ' + zone}</p>;}export default function App() { const [zone, setZone] = useState('north'); return <main><label>Station <select value={zone} onChange={e => setZone(e.target.value)}> <option value="north">North</option><option value="south">South</option></select></label> <Timetable zone={zone} /> <button onClick={() => queue.resolve('north')}>Deliver north</button> <button onClick={() => queue.resolve('south')}>Deliver south</button> </main>;}In a non-StrictMode test mount north, switch south, deliver south, then north. South remains visible because the old callback is no longer accepted. The same component stays mounted. Tagging the result with its zone avoids showing another station’s timetable while waiting. Revisiting a previously loaded zone can display that retained result until a new delivery; a production cache needs an explicit freshness policy. The guard also matters if the component unmounts. For a root StrictMode test, drain each pending delivery until the active request resolves: the fake queue intentionally retains ignored requests. This bounded fixture models out-of-order results without network or real timers. Ignoring a result does not cancel the underlying work; in a real cancellable request use the supported cancellation API as appropriate and guard stale results. A real request also needs error handling, caching and loading policies. Framework data loaders may be preferable. Do not declare the Effect setup itself async because it must return cleanup or nothing, not a Promise.
Practice
Using the queue alone, start north, deactivate its callback, start south and deliver in reverse order. Assert only south reaches the consumer. Then deactivate south before a new delivery and verify no update. Explain the difference between ignoring a result and cancelling its underlying operation.