Enforce skeleton
When a component renders nothing while it loads, the content pops in afterwards and pushes everything below it. A skeleton with the final size keeps the layout still.
12345{ "rules": { "systomic/enforce-skeleton": "warn" }}See the difference
Account
{data && <span>{data.name}</span>}Account
{data ? <span>{data.name}</span> : <Skeleton />}What doesn't work
3 findings12345678910111213141516171819202122232425262728import { useQuery } from "@tanstack/react-query";import { Suspense } from "react";
declare function fetchUser(): Promise<{ name: string; plan: string }>;declare function Feed(): React.ReactNode;
export function AccountName() { const { data } = useQuery({ queryKey: ["user"], queryFn: fetchUser }); return ( <p className="text-sm"> {data && <span>{data.name}</span>} </p> );}
export function PlanBadge() { const { data, isPending } = useQuery({ queryKey: ["user"], queryFn: fetchUser }); if (isPending) return null; return <span className="rounded-full px-2">{data?.plan}</span>;}
export function Home() { return ( <Suspense fallback={null}> <Feed /> </Suspense> );}systomic/enforce-skeleton data renders nothing while loading, then the content pops in and shifts the layout. Render a skeleton with the final size while loading.
systomic/enforce-skeleton Returning nothing while loading (isPending) makes the component pop in and shift the layout. Return a skeleton with the final size.
systomic/enforce-skeleton <Suspense> with fallback={null} renders nothing while loading, then the content pops in and shifts the layout. Pass a skeleton: fallback={<…Skeleton />}.
What works
No findings12345678910111213141516171819202122232425import { useQuery } from "@tanstack/react-query";import { Suspense } from "react";
declare function fetchUser(): Promise<{ name: string; plan: string; bio?: string }>;declare function Feed(): React.ReactNode;declare function Skeleton(props: { className?: string }): React.ReactNode;
/** The skeleton holds the line's size while loading. */export function AccountName() { const { data } = useQuery({ queryKey: ["user"], queryFn: fetchUser }); return <p className="text-sm">{data ? <span>{data.name}</span> : <Skeleton className="h-4 w-24" />}</p>;}
/** Optional content after loading is fine: the skeleton is decided above. */export function Bio({ user }: { user: { bio?: string } }) { return user.bio ? <p>{user.bio}</p> : null;}
export function Home() { return ( <Suspense fallback={<Skeleton className="h-96" />}> <Feed /> </Suspense> );}Details
Content that is still loading renders a skeleton with its final size, not nothing. When a component renders nothing while loading, the content pops in afterwards and pushes everything around it: a layout shift.
1234const { data } = useQuery(userQuery);
{data && <span>{data.name}</span>} // reported{data ? <span>{data.name}</span> : <Skeleton className="h-4 w-24" />} // fine
datarenders nothing while loading, then the content pops in and shifts the layout. Render a skeleton with the final size while loading.
How it decides
The rule evaluates each condition as it would be while loading:
- loading flags are
true:isLoading,isPending,isFetching,loading,isUserLoading,query.isPending, … (loadingPattern), - data from a query hook is
undefined:data(also renamed,{ data: user }) fromuseQuery,useInfiniteQuery,useSWR, tRPCapi.x.useQuery(),useFetch…,useLiveQuery,query.data, and Convex-styleconst tasks = useQuery(…), plus hooks inhooks, statusis"pending", andquery.isSuccessisfalse; error state (query.isError,error,failureReason,status === "error", …) is unknown, as it is also falsy once the data is loaded.
Anything else is unknown. Only when the result is certain, and the branch shown while loading renders nothing, is it reported:
{data && <Content />},{!isLoading && <Content />},{data?.user && <Avatar />},{data && isAdmin && <Badge />},{isLoading ? null : <Content />},{data ? <Content /> : undefined},{status === "pending" ? null : …},{tasks !== undefined && …},- early returns in components:
if (!data) return null,if (isPending) return;, <Suspense>without afallback, or withfallback={null}.
Not reported
- Loading indicators and skeletons:
{isLoading && <Spinner />},isLoading ? <Skeleton /> : …. - Conditions that don’t depend on loading: optional content (
user.bio ? … : null), errors ({query.isError && <ErrorMessage />}), local state, props (if (!user) return null). - Empty states:
{data?.length === 0 && <EmptyState />}only shows after loading, by design. - Suspense hooks (
useSuspenseQuery), which always have data. - Overlays (
{data && <Dialog />}): they don’t shift the layout. Seeno-conditional-overlay.
| Option | Default |
|---|---|
hooks |
[]: extra hooks whose result (or its data) is undefined while loading, e.g. ["useProject"] |
loadingPattern |
regex for loading flags, replaces the default |
ignore |
[]: file globs relative to the nearest package.json; see monorepos |