Stan aplikacji
state/fetch-effect-cleanup
An effect that fetches into state returns a cleanup that ignores or aborts stale responses.
umiarkowanastatycznefe-data-fetching
Dlaczego
When the input changes quickly (typing in a search box, clicking through pages), several requests are in flight and their answers arrive in any order. Without a cleanup, the last answer to arrive wins, so the page can show results for "hel" under the query "hello". A response that lands after the component unmounted also writes into a dead component.
Poprawka
Return a cleanup: let ignore = false … if (!ignore) setResults(json) … return () => { ignore = true }, or pass an AbortController signal to fetch and call abort() in the cleanup. Better still, load page data with the framework (Server Components, route loaders) or a server-state library (TanStack Query, SWR), which handle races, caching and deduplication.
Przykłady
// bad
useEffect(() => {
fetch(`/api/search?q=${query}`).then((r) => r.json()).then(setResults)
}, [query])
// good
useEffect(() => {
const controller = new AbortController()
getJson(`/api/search?q=${query}`, { signal: controller.signal }).then(setResults, onError)
return () => controller.abort()
}, [query])Źródła
Fakty
- Identyfikator
state/fetch-effect-cleanup- Waga
- umiarkowana
- Sprawdzanie
- statyczne sprawdzane w kodzie źródłowym przez
audit - Dotyczy plików
jsx- Tylko w projektach z
reactnextpreact- Uczy jej skill
fe-data-fetchingData fetching- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain state/fetch-effect-cleanup