Skip to main content
Version: 2.xx.xx

useCreateMany

useCreateMany is a modified version of react-query's useMutation for multiple create mutations.

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

Features

Usage

Let's say that we have a resource named categories.

https://api.fake-rest.refine.dev/categories
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "Virtual Invoice Avon",
},
];
}
type CategoryMutationResult = {
id: string;
title: string;
};

import { useCreateMany } from "@pankod/refine";

const { mutate } = useCreateMany<CategoryMutationResult>();

mutate({
resource: "categories",
values: [
{
title: "New Category",
},
{
title: "Another New Category",
},
],
});
tip

mutate can also accept lifecycle methods like onSuccess and onError.
Refer to react-query docs for further information.


After the mutation runs categories will be updated as below:

https://api.fake-rest.refine.dev/categories
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "Virtual Invoice Avon",
},
{
id: 3,
title: "New Category",
},
{
id: 4,
title: "Another New Category",
},
];
}
note

Queries that use /categories endpoint will be automatically invalidated to show the updated data. For example, data returned from useList will be automatically updated.

tip

useCreateMany returns react-query's useMutation result which includes a lot of properties, one of which being mutate.

info

Variables passed to mutate must have these types.

{
resource: string;
values: TVariables[] = {};
}

API

Properties

PropertyDescriptionTypeDefault
resource
Required
Resource name for API data interactionsstring
values
Required
Values for mutation functionTVariables[][{}]
successNotificationSuccessful Mutation notificationSuccessErrorNotification"Successfully created resources"
errorNotificationUnsuccessful Mutation notificationSuccessErrorNotification"There was an error creating resource (status code: statusCode)"
metaDataMetadata query for dataProviderMetaDataQuery{}

Type Parameters

PropertyDesriptionTypeDefault
TDataResult data of the mutation. Extends BaseRecordBaseRecordBaseRecord
TErrorCustom error object that extends HttpErrorHttpErrorHttpError
TVariablesValues for mutation function{}{}

Return value

DescriptionType
Result of the react-query's useMutationUseMutationResult<
{ data: TData[]},
TError,
{ resource: string; values: TVariables[]; },
unknown>