Refresh server rows
Fetch the rows in view again after the server changed them, and keep each row that has an open edit or a save in flight.
server.refresh() calls fetchRows again for the rows in view. Use it after the result behind the same spec changes, for example after a server-side import. The grid keeps the old rows on screen until the answers land.
Refresh the rows in view
This grid imports orders on the server, then refreshes:
"use client";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import { useDataGridServerRows } from "@/components/data-grid-server/data-grid-server";
type Order = { id: string; customer: string };
const columns = defineColumns<Order>()([
{ id: "customer", header: "Customer", accessorKey: "customer" },
]);
export function OrdersGrid() {
const server = useDataGridServerRows<Order>({
mode: "scroll",
fetchRows: async ({ start, end, signal }) => {
const query = `offset=${start}&limit=${end - start}`;
const response = await fetch(`/api/orders?${query}`, { signal });
const answer = await response.json();
return { rows: answer.rows, totalRowCount: answer.total };
},
});
async function importOrders() {
await fetch("/api/orders/import", { method: "POST" });
server.refresh();
}
return (
<>
<button type="button" onClick={importOrders}>
Import
</button>
<DataGrid {...server.gridProps} columns={columns} className="h-96" />
</>
);
}Each mode fetches different rows:
- Pages mode fetches the current page again.
- Scroll mode fetches the blocks around the rendered rows and drops every other loaded block.
- A sequential source fetches again from the first block, up to the rendered rows.
Keep rows with pending changes
A refresh keeps a row that has an open edit or a save in flight. The row keeps its local value. A pending insert or delete is one whose onSave promise has not settled.
- If the answer carries a row with a pending delete, the row stays gone.
- If the answer lacks a pending insert and no answer row sits in its slot, the insert returns to that slot.
- If an answer row sits in the slot, the insert stays hidden.
- Once no pending insert or delete remains, the hook refreshes again. Pages mode skips this refresh when
refreshAfterSaveisfalse. Scroll mode with random access runs it in every case. - A refresh of a sequential source that ends before a pending insert can drop it until its save settles.
In pages mode, a pending insert stays at the end of the page. The page can then hold more than pageSize rows until the save settles.
gridProps.plugins mounts the guard that applies these rules, then the plugins of the plugins option. A plugins prop of your own after the spread mounts next to the guard.
Let the hook refresh
The hook calls refresh() itself after saves settle (refreshAfterSave) and, in scroll mode with random access, after a row operation. See Save edits. A timer refreshes too: see Refresh on a timer.
Refresh on a timer
Set revalidateIntervalMs (milliseconds, at least 1000) to call refresh() on a timer while the page is visible. The default is off. Both modes take the option.
- A tick waits for a request in flight, and every refresh restarts the clock.
- A sequential source skips a tick while the rendered rows plus
overscanend beyond 10 blocks. - Pages mode also drops a prefetched page on a tick. See Tune page loading.
When live pushes leave rows out of order for the server's sort, the hook refetches one second after a push. Set reshapeDelayMs to change the delay. See Push into a server-ordered view.
Related
- Tune row retention: cap and evict loaded rows
- Save edits:
refreshAfterSaveand its policies - Push into a server-ordered view:
reshape()andreshapeDelayMs - Server rows options:
refreshandretry