Skip to main content
Version: 3.xx.xx

useSelect

useSelect hook allows you to manage any select (like a Html <select> tag, React Select, etc...) component. Since it is designed as headless, It expects you to handle the UI.

Usage

We'll demonstrate how to get data at /categories endpoint from https://api.fake-rest.refine.dev REST API.

https://api.fake-rest.refine.dev/categories
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "Virtual Invoice Avon",
},
{
id: 3,
title: "Unbranded",
},
];
}
pages/posts/create.tsx
import { useSelect } from "@pankod/refine-core";

export const DummyList = () => {
const { options } = useSelect<ICategory>({
resource: "categories",
});

return (
<select>
{options?.map((category) => (
<option key={category.value} value={category.value}>
{category.label}
</option>
))}
</select>
);
};

interface ICategory {
id: number;
title: string;
}

useSelect uses the useList hook for fetching data. Refer to useList hook for details.

Options

resource

const { options } = useSelect({
resource: "categories",
});

resource property determines API resource endpoint to fetch records from dataProvider. It returns properly configured options values for select options.

defaultValue

const { options } = useSelect({
resource: "categories",
defaultValue: "1",
});

Adds extra options to <select> component. It uses useMany so defaultValue can be an array of strings like follows.

defaultValue: ["1", "2"],

Refer to the useMany documentation for detailed usage.

tip

Can use defaultValue property when edit a record in Edit page.

optionLabel and optionValue

const { options } = useSelect({
resource: "categories",
optionLabel: "title",
optionValue: "id",
});

Allows you to change the values and appearance of your options. Default values are optionLabel = "title" and optionValue = "id".

tip

Supports use with optionLabel and optionValue Object path syntax.

const { options } = useSelect({
resource: "categories",
optionLabel: "nested.title",
optionValue: "nested.id",
});

filters

const { options } = useSelect({
resource: "categories",
filters: [
{
field: "isActive",
operator: "eq",
value: true,
},
],
});

It allows us to add some filters while fetching the data. For example, if you want to list only the active records.

sort

const { options } = useSelect({
resource: "categories",
sort: [
{
field: "title",
order: "asc",
},
],
});

It allows us to sort the options. For example, if you want to sort your list according to title by ascending.

fetchSize

const { options } = useSelect({
resource: "categories",
fetchSize: 20,
});

Amount of records to fetch in select box.

onSearch

It allows us to AutoComplete the options.

const { options } = useSelect({
resource: "categories",
onSearch: (value) => [
{
field: "title",
operator: "containss",
value,
},
],
});
tip

The HTML select tag does not natively support AutoComplete. If AutoComplete is desired, it can be used with React Select or use-select.

If defined, it allows us to override the filters to use when fetching list of records. Thus, it . It should return CrudFilters.

queryOptions

const { options } = useSelect({
resource: "categories",
queryOptions: {
onError: () => {
console.log("triggers when on query return Error");
},
},
});

useQuery options can be set by passing queryOptions property.

defaultValueQueryOptions

When the defaultValue property is given, the useMany data hook is called for the selected records. With this property, you can change the options of this query. If not given, the values given in queryOptions will be used.

const { options } = useSelect({
resource: "categories",
defaultValueQueryOptions: {
onSuccess: (data) => {
console.log("triggers when on query return on success");
},
},
});

useQuery options for default value query can be set by passing queryOptions property.

API Reference

Properties

PropertyDescriptionTypeDefault
resource
Required
Resource name for API data interactionsstring
defaultValueAdds extra optionsBaseKey | BaseKey[]
optionValueSet the option's valuestring"id"
optionLabelSet the option's label valuestring"title"
filtersAdd filters while fetching the dataCrudFilters
sortAllow us to sort the optionsCrudSorting
debounceThe number of milliseconds to delaynumber300
queryOptionsreact-query useQuery options UseQueryOptions<GetListResponse<TData>, TError>
defaultValueQueryOptionsreact-query useQuery options UseQueryOptions<GetManyResponse<TData>, TError>
fetchSizeAmount of records to fetch in select box list.numberundefined
onSearchIf defined, this callback allows us to override all filters for every search request.(value: string) => CrudFilters | Promise<CrudFilters>undefined
metaDataMetadata query for dataProviderMetaDataQuery{}
dataProviderNameIf there is more than one dataProvider, you should use the dataProviderName that you will use.stringdefault
liveModeWhether to update data automatically ("auto") or not ("manual") if a related live event is received. The "off" value is used to avoid creating a subscription."auto" | "manual" | "off""off"
liveParamsParams to pass to liveProvider's subscribe method if liveMode is enabled.{ ids?: BaseKey[]; [key: string]: any; }undefined
onLiveEventCallback to handle all related live events of this hook.(event: LiveEvent) => voidundefined

Return values

PropertyDescriptionType
optionsIt returns possible options{ label: string; value: string }
queryResultResult of the query of a recordQueryObserverResult<{ data: TData }>
defaultValueQueryResultResult of the query of a defaultValue recordQueryObserverResult<{ data: TData }>
defaultValueQueryOnSuccessDefault value onSuccess method() => void

Live StackBlitz Example