Stan aplikacji
state/server-state-cache
Server data comes from the framework loader or a server-state library, not fetch plus useState and loading flags in each component.
umiarkowanaręcznefe-data-fetching
Dlaczego
Each hand-written fetch + useState + isLoading block re-implements caching, deduplication, race handling, retries and refetching, usually only partly. Two components that need the same data request it twice, the page renders a spinner where server rendering could have sent the content, and requests run one after another (waterfalls).
Poprawka
Use what the stack provides: Server Components or route loaders (Next.js, React Router), load (SvelteKit), useFetch/useAsyncData (Nuxt), httpResource or resolvers (Angular); in a client-only app, TanStack Query, SWR or RTK Query. Start independent requests together. Keep a raw fetch for event handlers (submitting a form) and for code outside components.
Przykłady
// bad: every page repeats this
const [user, setUser] = useState(null)
const [loading, setLoading] = useState(true)
useEffect(() => { getUser(id).then(setUser).finally(() => setLoading(false)) }, [id])
// good
const { data: user, error, isPending } = useQuery({ queryKey: ['user', id], queryFn: () => getUser(id) })Źródła
Fakty
- Identyfikator
state/server-state-cache- Waga
- umiarkowana
- Sprawdzanie
- ręczne ocenia człowiek; raport podaje to jako listę kontrolną
- Dotyczy plików
jsxvuesvelteangular- Uczy jej skill
fe-data-fetchingData fetching- Od wersji
- 0.1.0
Przeczytaj w terminalu
npx github:dawidolko/frontend-skills explain state/server-state-cache