Next.js

next/no-async-client-component

A Client Component is never an async function; fetch in a Server Component parent or read a promise with use().

poważnastatycznefe-nextjs

Dlaczego

Only Server Components can be async. An async component in a 'use client' file returns a promise on every render, which React does not support on the client: the component suspends forever, re-fetches in a loop or throws, depending on how it is reached.

Poprawka

Do the await in a Server Component and pass the result down as props, or start the request on the server, pass the promise, and read it in the client component with use(promise) inside <Suspense>. For data that must load in the browser, use a data library (TanStack Query, SWR) or an effect with cleanup.

Przykłady

// bad
'use client'
export default async function Cart() { const items = await getCart(); return <CartList items={items} /> }
// good
'use client'
export default function Cart({ itemsPromise }) { const items = use(itemsPromise); return <CartList items={items} /> }

Źródła

Fakty

Identyfikator
next/no-async-client-component
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx
Tylko w projektach z
next
Uczy jej skill
fe-nextjs Next.js App Router
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain next/no-async-client-component