Skip to main content
Version: 3.xx.xx

useList

useList is a modified version of react-query's useQuery used for retrieving items from a resource with pagination, sort, and filter configurations.

It uses the getList method as the query function from the dataProvider which is passed to <Refine>.

Usage

Let's say that we have a resource named posts

https://api.fake-rest.refine.dev/posts
{
[
{
id: 1,
title: "E-business",
status: "draft",
},
{
id: 2,
title: "Virtual Invoice Avon",
status: "published",
},
{
id: 3,
title: "Powerful Crypto",
status: "rejected",
},
];
}

useList passes the query configuration to getList method from the dataProvider. We will be using the dataProvider from @pankod/refine-simple-rest

First of all, we will use useList without passing any query configurations.

import { useList } from "@pankod/refine-core";

type IPost = {
id: number;
title: string;
status: "rejected" | "published" | "draft";
};

const postListQueryResult = useList<IPost>({ resource: "posts" });
postListQueryResult.data
{
data: [
{
id: 3,
title: "Powerful Crypto",
status: "rejected"
},
{
id: 2,
title: "Virtual Invoice Avon",
status: "published"
},
{
id: 1,
title: "E-business",
status: "draft"
},
],
total: 3
}

Although we didn't pass any sort order configurations to useList, data comes in descending order according to id since getList has default values for sort order:

{
sort: [{ order: "desc", field: "id" }];
}
important

getList also has default values for pagination:

{
pagination: { current: 1, pageSize: 10 }
}
caution

If you want to create your own getList method, it will automatically implement default query configurations since useList can work with no configuration parameters.


Query Configuration

pagination

Allows us to set page and items per page values.

For example imagine that we have 1000 post records:

import { useList } from "@pankod/refine-core";

const postListQueryResult = useList<IPost>({
resource: "posts",
config: {
pagination: { current: 3, pageSize: 8 },
},
});

Listing will start from page 3 showing 8 records.


sort

Allows us to sort records by the speficified order and field.

import { useList } from "@pankod/refine-core";

const postListQueryResult = useList<IPost>({
resource: "posts",
config: {
sort: [{ order: "asc", field: "title" }],
},
});
postListQueryResult.data
{
data: [
{
id: 1,
title: "E-business",
status: "draft"
},
{
id: 3,
title: "Powerful Crypto",
status: "rejected"
},
{
id: 2,
title: "Virtual Invoice Avon",
status: "published"
},
],
total: 3
}

Listing starts from ascending alphabetical order on the title field.


filters

Allows us to filter queries using refine's filter operators. It is configured via field, operator and value properites.

import { useList } from "@pankod/refine-core";

const postListQueryResult = useList<IPost>({
resource: "posts",
config: {
filters: [
{
field: "status",
operator: "eq",
value: "rejected",
},
],
},
});
postListQueryResult.data
{
data: [
{
id: 3,
title: "Powerful Crypto",
status: "rejected"
},
],
total: 1
}

Only lists records whose status equals to "rejected".


Supported operators

FilterDescription
eqEqual
neNot equal
ltLess than
gtGreater than
lteLess than or equal to
gteGreater than or equal to
inIncluded in an array
ninNot included in an array
containsContains
ncontainsDoesn't contain
containssContains, case sensitive
ncontainssDoesn't contain, case sensitive
nullIs null or not null

tip

useList can also accept all useQuery options as a third parameter.
Refer to react-query docs for further information. β†’

  • For example, to disable query from running automatically you can set enabled to false.
import { useList } from "@pankod/refine-core";

const postListQueryResult = useList<IPost>({
resource: "posts",
queryOptions: { enabled: false },
});

tip

useList returns the result of react-query's useQuery which includes many properties such as isLoading and isFetching. Refer to react-query docs for further information. β†’

API

Properties

PropertyDescriptionType
resource
Required
Resource name for API data interactionsstring
configConfiguration for pagination, sorting and filteringUseListConfig
queryOptionsreact-query's useQuery options UseQueryOptions<
{ data: TData[]; },
TError>
successNotificationSuccessful Query notificationSuccessErrorNotificationfalse
errorNotificationUnsuccessful Query notificationSuccessErrorNotification"Error (status code: statusCode)"
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

Config parameters

interface UseListConfig {
pagination?: {
current?: number;
pageSize?: number;
};
sort?: Array<{
field: string;
order: "asc" | "desc";
}>;
filters?: Array<{
field: string;
operator: CrudOperators;
value: any;
}>;
}

Type Parameters

PropertyDesriptionTypeDefault
TDataResult data of the query. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError

Return values

DescriptionType
Result of the react-query's useQueryQueryObserverResult<{
data: TData[];
total: number; },
TError>