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

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