Custom React Hook
useApiOptions
useApiOptions powers SelectApiField and remote autocompletes. It handles debounced search queries, infinite scroll loading, caching, and mapping backend keys to { key, value } options.
Debounced search queries (default: 400ms)
Infinite scroll pagination (loadingMore state)
Custom transformation via onFormatData callback
Hook Parameters
| Property | Type | Default | Description |
|---|---|---|---|
| apiUrl | string | — | Remote endpoint to query. |
| debounceMs | number | 400 | Search debounce delay in milliseconds. |
| labelPath | string | "value" | JSON key for option label. |
| valuePath | string | "key" | JSON key for option value. |
Return Object Properties
| Property | Type | Default | Description |
|---|---|---|---|
| options | OptionType[] | — | Formatted array of { key, value } options. |
| loading | boolean | — | Initial query loading state. |
| loadingMore | boolean | — | Loading state during infinite scroll. |
| onSearch | (text: string) => void | — | Search callback to trigger debounced query. |
Practical Hook Examples & Workflows
Explore quickstart usage, full parameter integration, and production data workflows.
Standalone Remote Select with useApiOptions: Hook integration with Ant Design's standard Select component.
useApiOptions.select.tsxtsx
1import React from "react";2import { useApiOptions } from "antd-dynamic-ui";3import { Select } from "antd";45export default function DepartmentSelect() {6 const { options, loading, onSearch } = useApiOptions({7 apiUrl: "/api/v1/departments",8 labelPath: "deptName",9 valuePath: "deptId",10 debounceMs: 300,11 });1213 return (14 <Select15 showSearch16 placeholder="Select department"17 loading={loading}18 onSearch={onSearch}19 options={options.map((o) => ({ label: o.value, value: o.key }))}20 className="w-72"21 />22 );23}