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
{
[
{
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" });
{
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" }];
}
getList
also has default values for pagination:
{
pagination: { current: 1, pageSize: 10 }
}
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" }],
},
});
{
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",
},
],
},
});
{
data: [
{
id: 3,
title: "Powerful Crypto",
status: "rejected"
},
],
total: 1
}
Only lists records whose
status
equals to "rejected".
Supported operators
Filter | Description |
---|---|
eq | Equal |
ne | Not equal |
lt | Less than |
gt | Greater than |
lte | Less than or equal to |
gte | Greater than or equal to |
in | Included in an array |
nin | Not included in an array |
contains | Contains |
ncontains | Doesn't contain |
containss | Contains, case sensitive |
ncontainss | Doesn't contain, case sensitive |
null | Is null or not null |
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
tofalse
.
import { useList } from "@pankod/refine-core";
const postListQueryResult = useList<IPost>({
resource: "posts",
queryOptions: { enabled: false },
});
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β
Config parametersβ
interface UseListConfig {
hasPagination?: boolean;
pagination?: {
current?: number;
pageSize?: number;
};
sort?: Array<{
field: string;
order: "asc" | "desc";
}>;
filters?: Array<{
field: string;
operator: CrudOperators;
value: any;
}>;
}
Type Parametersβ
Property | Desription | Type | Default |
---|---|---|---|
TData | Result data of the query. Extends BaseRecord | BaseRecord | BaseRecord |
TError | Custom error object that extends HttpError | HttpError | HttpError |
Return valuesβ
Description | Type |
---|---|
Result of the react-query 's useQuery | QueryObserverResult<{ data: TData[]; total: number; }, TError> |