Server-side multi-select across pages
For a React form backed by a paginated API, store selected IDs independently of the current page. A selected customer or part should not disappear when the search term changes or a different page loads.
LookupSelect tracks draft and committed selection identities independently of the displayed grid. A remote query returns only its matching page. The component preserves the rows it has selected across pages; IDs loaded from an existing form may need explicit hydration.
API contract
Section titled “API contract”Use one endpoint for a filtered and sorted page, and another endpoint for the records already associated with the form. Validate these requests on the server and restrict both endpoints to records the current user may read.
This example assumes the parent loads the order and its associated parts before rendering the field.
import { useState } from 'react';import { LookupSelect } from 'react-lookup-select';import type { ColumnDef, DataSourceFn, RowId } from 'react-lookup-select';import 'react-lookup-select/styles.css';
type Part = { id: number; code: string; name: string };const columns: ColumnDef<Part>[] = [ { key: 'code', title: 'SKU', sortable: true }, { key: 'name', title: 'Name', sortable: true },];
const loadPage: DataSourceFn<Part> = async query => { const params = new URLSearchParams({ page: String(query.page), pageSize: String(query.pageSize), search: query.search ?? '', sortBy: query.sortBy ?? '', sortDir: query.sortDir ?? '', }); const response = await fetch('/api/parts?' + params); if (!response.ok) throw new Error('Could not load parts'); return response.json();};
export function OrderParts({ initialParts }: { initialParts: Part[] }) { const [ids, setIds] = useState<RowId[]>(initialParts.map(p => p.id)); const [selectedRows, setSelectedRows] = useState(initialParts);
return ( <LookupSelect mode="multiple" dataSource={loadPage} columns={columns} mapper={{ getId: part => part.id, getText: part => part.name }} valueFormat="ids" value={ids} hydrationRows={selectedRows} returnShape="row" onChange={rows => { setIds(rows.map(part => part.id)); setSelectedRows(rows); }} /> );}The two state values serve different purposes: IDs are the form value; selectedRows supplies the display and output payload when a selected part is absent from the current page. The API URL is illustrative and must be implemented in your application.
What happens when an ID cannot be loaded?
Section titled “What happens when an ID cannot be loaded?”It remains pending. For full-row or custom outputs, the picker cannot emit a complete value until the row is available or the pending ID is cleared. If the parent has an ID and label but no complete row, ID-text output can use the supplied label instead.
Do not silently drop unknown IDs: make the missing record visible and let the user decide whether to remove it.
Test the workflow
Section titled “Test the workflow”Open the playground, select remote data and 100 records, choose multiple selection and select records from two pages. Apply to inspect the committed output. Open it again, make a change and cancel; the committed output stays unchanged.
Related: data sources, value formats and selection hydration.