跳到主要内容

useUpdate

useUpdate is a modified version of react-query's useMutation for update mutations.

It uses update 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: number;
title: string;
};

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

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

mutate({
resource: "categories",
id: 2,
values: { title: "New Category Title" },
});
提示

mutate can also accept lifecycle methods like onSuccess and onError.

mutate(
{
resource: "categories",
id: 2,
values: { title: "New Category Title" },
},
{
onError: (error, variables, context) => {
// An error happened!
},
onSuccess: (data, variables, context) => {
// Let's celebrate!
},
},
);

Refer to react-query docs for further information.


After mutation runs, categories will be updated as below:

https://api.fake-rest.refine.dev/categories
{
[
{
id: 1,
title: "E-business",
},
{
id: 2,
title: "New Category Title",
},
];
}

备注

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

提示

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

警告

Values passed to mutate must have these types.

{
resource: string;
id: BaseKey;
values: TVariables = {};
mutationMode?: MutationMode;
undoableTimeout?: number;
onCancel?: (cancelMutation: () => void) => void;
}

Mutation mode

Mutation mode determines which mode mutation runs with.

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

const { mutate } = useUpdate();

mutate({
resource: "categories",
id: 2,
values: { title: "New Category Title" },
mutationMode: "optimistic",
});

Refer to mutation mode docs for further information.

Creating a custom method for cancelling mutations

You can pass a custom cancel callback to useUpdate. This callback will be triggered instead of the default one when undo button is clicked when mutationMode = "undoable".

警告

Default behaviour on undo action includes notifications. If a custom callback is passed, this notification will not appear.

危险

Passed callback will receive a function that actually cancels the mutation. Don't forget to run this function to cancel the mutation on undoable mode.

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

const customOnCancel = (cancelMutation: () => void) => {
cancelMutation();
// rest of custom cancel logic...
};

const { mutate } = useUpdate();

mutate({
resource: "categories",
id: 2,
values: { title: "New Category Title" },
mutationMode: "undoable",
undoableTimeout: 7500,
onCancel: customOnCancel,
});

After 7.5 seconds the mutation will be executed. The mutation can be cancelled within that 7.5 seconds. If cancelled, customOnCancel will be executed and the request will not be sent.


API

Properties

PropertyDescriptionTypeDefault
resource
Required
Resource name for API data interactionsstring
id
Required
id for mutation functionBaseKey
values
Required
Values for mutation functionTVariables{}
mutationModeDetermines when mutations are executed "pessimistic | "optimistic | "undoable""pessimistic"*
undoableTimeoutDuration to wait before executing the mutation when mutationMode = "undoable"number5000ms*
onCancelCallback that runs when undo button is clicked on mutationMode = "undoable"(cancelMutation: () => void) => void
successNotificationSuccessful Mutation notificationSuccessErrorNotification"Successfully updated resource"
errorNotificationUnsuccessful Mutation notificationSuccessErrorNotification"Error when updating resource (status code: statusCode)"
metaDataMetadata query for dataProviderMetaDataQuery{}
dataProviderNameIf there is more than one dataProvider, you should use the dataProviderName that you will use.stringdefault
invalidatesYou can use it to manage the invalidations that will occur at the end of the mutation.all, resourceAll, list, many, detail, false["list", "many", "detail"]

*: These props have default values in RefineContext and can also be set on <Refine> component. useUpdate will use what's passed to <Refine> as default, but a local value will override it.


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; id: BaseKey; values: TVariables; },
UpdateContext>
*

* UpdateContext is an internal type.