Skip to main content
Version: 4.xx.xx

Mutation Mode

Overview

Mutation mode determines which mode the mutation runs with. Mutations can run under three different modes: pessimistic, optimistic and undoable. Each mode corresponds to a different type of user experience.

Modes

We'll show usages of modes with editing a record examples.

pessimistic

The mutation runs immediately. Redirection and UI updates are executed after the mutation returns successfully.

You can experience an example of this below using the edit page.

localhost:5173/posts

When the user clicks on save button, request to the API happens directly and after successful response, list page updates with newly edited record.


optimistic

The mutation is applied locally, redirection and UI updates are executed immediately as if the mutation is successful. If mutation returns with error, UI updates to show data prior to the mutation.

localhost:5173/posts

When the user clicks on save button, request to the API happens directly and list page updates with edited data immediately without waiting API response.


undoable

The mutation is applied locally, redirection and UI updates are executed immediately as if the mutation is successful. Waits for a customizable amount of timeout period before mutation is applied. During the timeout, mutation can be cancelled from the notification with an undo button and UI will revert back accordingly.

localhost:5173/posts

When the user clicks on save button, request isn't sent to API immediately however list page updates with edited data. It waits for a period of time while the user can cancel the mutation. If the mutation is cancelled, locally applied edit is undone.

Usage

Mutation mode can be set application-wide in <Refine> component.

App.tsx
<Refine
...
options={{ mutationMode: "optimistic" }}
/>

Its default value is pessimistic.


It can also be set in supported data hooks and form hooks for fine-grained configuration.

import { useUpdate } from "@refinedev/core";

const { mutate } = useUpdate();

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

Mutation mode passed to <Refine> will be overridden by the mutation mode passed to data or form hooks and components.

Supported data hooks

Example

Run on your local
npm create refine-app@latest -- --example form-antd-mutation-mode