Loading state example
Show a skeleton, a progress bar, or the empty state with the loading prop, and replace each display with your own.
The loading prop and the number of rows in view decide what the grid shows. In the demo, the Loading switch sets loading, and the Rows present switch adds or removes the rows. Together they reach every state in the table.
Toggle both switches: Loading with no rows shows a viewport-filling skeleton, Loading with rows present shows a slim bar under the header instead, and rows-off + loading-off is the ordinary empty state.
pnpm dlx shadcn@latest add @gridcn/data-grid-loading-demo
| Rows in view | loading | The grid shows |
|---|---|---|
| none | true | Skeleton rows that fill the viewport. |
| some | true | A slim indeterminate bar under the header. The rows stay on screen. |
| none | false | The empty state, with the text labels.grid.emptyState, or labels.grid.noResults when a filter or a server search hides every row. |
Where the data comes from
loading changes what the grid shows and fetches nothing. Pass the flag from the code that owns your request. The server rows hook returns loading in gridProps.
Customizing the look
While loading is true, loadingState replaces the skeleton and the bar. It takes a node, or a function that receives { reason }. If no rows are in view, the reason is "no-data". If rows are in view, it is "rows-present". The grid renders the result under the header at full width, and the result owns its accessibility.
emptyState takes the same two forms. If data is empty and no server filter or search is active, its reason is "no-data". In every other case the reason is "filtered".
The sample passes data without onDataChange, so it sets readOnly:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Order = { id: string; customer: string };
const columns = defineColumns<Order>()([
{ id: "customer", header: "Customer", accessorKey: "customer" },
]);
export function Orders({ rows, loading }: { rows: Order[]; loading: boolean }) {
return (
<DataGrid
className="h-80"
data={rows}
columns={columns}
readOnly
loading={loading}
loadingState={({ reason }) => (
<p className="p-4 text-sm">
{reason === "no-data" ? "Fetching orders…" : "Refreshing…"}
</p>
)}
emptyState={({ reason }) =>
reason === "filtered" ? "No order matches" : "No orders yet"
}
/>
);
}Related
- Loading state: the props, the error state, and server mode
- Load rows while scrolling: skeleton cells for rows that are not loaded yet
- Styling example: class names on shadcn tokens