useExport
useExport
hook allows you to export data as a CSV
file. It calls the getList
method of your data provider and downloads the data as a CSV
file.
Internally, it uses export-to-csv
to create the CSV
file.
Basic Usageβ
Here is a basic usage example of the useExport
hook:
import { useExport } from "@refinedev/core";
interface IPost {
id: number;
slug: string;
title: string;
content: string;
}
export const PostList: React.FC = () => {
const { triggerExport } = useExport<IPost>();
return <button onClick={triggerExport}>Export Button</button>;
};
Propertiesβ
resource
β
Default: Read from the current route
Determines which resource is passed to the getList
method of your data provider.
useExport({
resource: "posts",
});
If you have multiple resources with the same name, you can pass the identifier
instead of the name
of the resource. It will only be used as the main matching key for the resource, data provider methods will still work with the name
of the resource defined in the <Refine/>
component.
For more information, refer to the
identifier
of the<Refine/>
component documentation β
mapData
β
If you want to map the data before exporting it, you can use the mapData
property.
interface IPost {
id: number;
slug: string;
title: string;
content: string;
category: {
id: number;
};
}
useExport<IPost>({
mapData: (item) => {
return {
id: item.id,
slug: item.slug,
title: item.title,
content: item.content,
categoryId: item.category.id,
};
},
});
sorters
β
If you want to sort the data before exporting it, you can use the sorters
property. It will be passed to the getList
method of your data provider.
For more information, refer to the
CrudSorting
interfaceβ
useExport({
sorters: [
{
field: "title",
order: "asc",
},
],
});
filters
β
If you want to filter the data before exporting it, you can use the filters
property. It will be passed to the getList
method of your data provider.
For more information, refer to the
CrudFilters
interface β
useExport({
filters: [
{
field: "title",
operator: "contains",
value: "foo",
},
],
});
maxItemCount
β
By default, the useExport
hook will export all the data. If you want to limit the number of items to be exported, you can use the maxItemCount
property.
useExport({
maxItemCount: 100,
});
pageSize
β
Requests to fetch data are made in batches of 20 by default. The pageSize
property determines the number of items to be fetched in each request.
useExport({
pageSize: 50,
});
exportOptions
β
You can pass additional options to the export-to-csv
package by using the exportOptions
property.
For more information, refer to the
ExportToCsv
options β
useExport({
exportOptions: {
filename: "posts",
},
});
meta
β
If you want to send additional data to the create
or createMany
method of your data provider, you can use the meta
property.
useExport({
meta: {
foo: "bar",
},
});
dataProviderName
β
If there is more than one dataProvider
, you can specify which one to use by passing the dataProviderName
prop. It is useful when you have a different data provider for different resources.
useExport({
dataProviderName: "second-data-provider",
});
onError
β
Callback function that is called when an error occurs while fetching data.
useExport({
onError: (error) => {
console.log(error);
},
});
resourceName
β
resourceName
Use resource
instead.
sorter
β
sorter
Use sorters
instead.
Return Valuesβ
triggerExport
β
A function that triggers the export process.
const { triggerExport } = useExport();
return <button onClick={triggerExport}>Export Button</button>;
isLoading
β
A boolean value that indicates whether the export process is in progress.
const { isLoading } = useExport();
return isLoading ? <div>Loading...</div> : <div>Loaded</div>;
FAQβ
Handling Relational Dataβ
A mapping function can be run on all entries before saving them, which is useful in cases where you need to reference relational data or save files in a specific format for processing in other applications.
Consider this endpoint containing some relational data:
[
{
"id": 2,
"title": "Et architecto et aut ex.",
"slug": "dolorum-et-quia",
"content": "Reprehenderit qui voluptatem in cum qui odio et.",
"category": {
"id": 35
},
"user": {
"id": 10
},
},
{
"id": 3,
"title": "Quam maiores officia suscipit quia vel asperiores nisi non excepturi.",
"slug": "delectus-laborum-provident",
"content": "Placeat eos esse.",
"category": {
"id": 4
},
"user": {
"id": 50
},
},
...
]
We have the category
and user
fields as possible relational data keys. Their data is not responsibility of this export operation.
If we want to save their id
's without any other related data, we can use a mapping function to save category.id
as categoryId
and user.id
as userId
.
useExport<IPost>({
mapData: (item) => {
return {
id: item.id,
title: item.title,
slug: item.slug,
content: item.content,
categoryId: item.category.id,
userId: item.user.id,
};
},
});
interface ICategory {
id: number;
title: string;
}
interface IPost {
id: number;
title: string;
content: string;
slug: string;
category: { id: number };
user: { id: number };
}
This will save the data as follows:
[
{
"id": 2,
"title": "Et architecto et aut ex.",
"slug": "dolorum-et-quia",
"content": "Reprehenderit qui voluptatem in cum qui odio et.",
"categoryId": 35,
"userId": 10
},
{
"id": 3,
"title": "Quam maiores officia suscipit quia vel asperiores nisi non excepturi.",
"slug": "delectus-laborum-provident",
"content": "Placeat eos esse.",
"categoryId": 4,
"userId": 50
},
...
]
API Referenceβ
Propertiesβ
Return Valuesβ
Key | Description | Type |
---|---|---|
isLoading | Shows true when there is an export process | boolean |
triggerExport | When invoked, starts the exporting process | async () => void |
Type Parametersβ
Property | Desription | Default |
---|---|---|
TData | Result type of the data query type that extends BaseRecord | BaseRecord |
TVariables | Values for params | any |