---
layout: default
title: "The EditBase Component"
storybook_path: ra-core-controller-editbase--default-title
---
# ``
`` is a headless variant of [``](./Edit.md): it fetches a record based on the URL, prepares a form submit handler, and renders its children inside an [`EditContext`](./useEditContext.md). Use it to build a custom edition page layout.
Contrary to [``](./Edit.md), it does not render the page layout, so no title, no actions, and no ``.
`` relies on the [`useEditController`](./useEditController.md) hook.
## Usage
Use `` to create a custom Edition view, with exactly the content you add as child and nothing else (no title, Card, or list of actions as in the `` component).
```jsx
import { EditBase, SelectInput, SimpleForm, TextInput, Title } from "react-admin";
import { Card, CardContent, Container } from "@mui/material";
export const BookEdit = () => (
);
```
## Props
| Prop | Required | Type | Default | Description
|--------------------------|----------|----------------------------------------------------------|----------|--------------------------------------------------------
| `authLoading` | Optional | `ReactNode` | | The component to render while checking for authentication and permissions
| `children` | Optional | `ReactNode` | | The components rendering the record fields
| `render` | Optional | `(props: EditControllerResult) => ReactNode` | | Alternative to children, a function that takes the EditController context and renders the form
| `disable Authentication` | Optional | `boolean` | | Set to `true` to disable the authentication check
| `error` | Optional | `ReactNode` | | The component to render when failing to load the record
| `id` | Optional | `string` | | The record identifier. If not provided, it will be deduced from the URL
| `loading` | Optional | `ReactNode` | | The component to render while loading the record to edit
| `mutationMode` | Optional | `undoable` | | The mutation mode
| `mutationOptions` | Optional | `ReactNode` | | The options to pass to the `useUpdate` hook
| `offline` | Optional | `ReactNode` | | The component to render when there is no connectivity and the record isn't in the cache
| `queryOptions` | Optional | `object` | | The options to pass to the `useGetOne` hook
| `redirectOnError` | Optional | `'list'` | `false` | `function` | `'list'` | The page to redirect to when an error occurs
| `transform` | Optional | `string` | | Transform the form data before calling `dataProvider.update()`
## `authLoading`
By default, `` renders the children while checking for authentication and permissions. You can display a component during this time via the `authLoading` prop:
```jsx
import { EditBase } from 'react-admin';
export const PostEdit = () => (
Checking for permissions...
}>
...
);
```
## `children`
`` renders its children wrapped by a `RecordContext`, so you can use any component that depends on such a context to be defined - including all [Inputs components](./Inputs.md).
For instance, to display several fields in a single line, you can use Material UI’s `` component:
{% raw %}
```jsx
import { EditBase, Form, DateInput, ReferenceInput, SaveButton, TextInput } from 'react-admin';
import { Grid } from '@mui/material';
const BookEdit = () => (
);
```
{% endraw %}
## `disableAuthentication`
By default, the `` component will automatically redirect the user to the login page if the user is not authenticated. If you want to disable this behavior and allow anonymous access to a show page, set the `disableAuthentication` prop to `true`.
```jsx
import { EditBase } from 'react-admin';
const PostEdit = () => (
...
);
```
## `error`
By default, `` redirects to the list when an error happens while loading the record to edit. You can render an error component via the `error` prop:
```jsx
import { EditBase } from 'react-admin';
export const PostEdit = () => (
Something went wrong while loading your post!}>
...
);
```
## `id`
By default, `` deduces the identifier of the record to show from the URL path. So under the `/posts/123/show` path, the `id` prop will be `123`. You may want to force a different identifier. In this case, pass a custom `id` prop.
```jsx
import { EditBase } from 'react-admin';
export const PostEdit = () => (
...
);
```
**Tip**: Pass both a custom `id` and a custom `resource` prop to use `` independently of the current URL. This even allows you to use more than one `` component in the same page.
## `loading`
By default, `` renders the children while loading the record to edit. You can display a component during this time via the `loading` prop:
```jsx
import { EditBase } from 'react-admin';
export const PostEdit = () => (
Loading the post...}>
...
);
```
## `mutationMode`
The `` component exposes a save method, which perform a "mutation" (i.e. they alter the data). React-admin offers three modes for mutations. The mode determines when the side effects (redirection, notifications, etc.) are executed:
* `pessimistic`: The mutation is passed to the dataProvider first. When the dataProvider returns successfully, the mutation is applied locally, and the side effects are executed.
* `optimistic`: The mutation is applied locally and the side effects are executed immediately. Then the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
* `undoable` (default): The mutation is applied locally and the side effects are executed immediately. Then a notification is shown with an undo button. If the user clicks on undo, the mutation is never sent to the dataProvider, and the page is refreshed. Otherwise, after a 5 seconds delay, the mutation is passed to the dataProvider. If the dataProvider returns successfully, nothing happens (as the mutation was already applied locally). If the dataProvider returns in error, the page is refreshed and an error notification is shown.
By default, pages using `` use the `undoable` mutation mode. This is part of the "optimistic rendering" strategy of react-admin ; it makes user interactions more reactive.
You can change this default by setting the `mutationMode` prop - and this affects both the Save and Delete buttons. For instance, to remove the ability to undo the changes, use the `optimistic` mode:
```jsx
import { EditBase } from 'react-admin';
const PostEdit = () => (
// ...
);
```
And to make the Save action blocking, and wait for the dataProvider response to continue, use the `pessimistic` mode:
```jsx
import { EditBase } from 'react-admin';
const PostEdit = () => (
// ...
);
```
## `mutationOptions`
`` calls `dataProvider.update()` via react-query's `useMutation` hook. You can customize the options you pass to this hook, e.g. to pass [a custom `meta`](./Actions.md#meta-parameter) to the `dataProvider.update()` call.
{% raw %}
```jsx
import { EditBase, SimpleForm } from 'react-admin';
const PostEdit = () => (
...
);
```
{% endraw %}
You can also use `mutationOptions` to override success or error side effects, by setting the `mutationOptions` prop. Refer to the [useMutation documentation](https://tanstack.com/query/v5/docs/framework/react/reference/useMutation) in the react-query website for a list of the possible options.
Let's see an example with the success side effect. By default, when the save action succeeds, react-admin shows a notification, and redirects to the list page. You can override this behavior and pass custom success side effects by providing a `mutationOptions` prop with an `onSuccess` key:
{% raw %}
```jsx
import * as React from 'react';
import { useNotify, useRefresh, useRedirect, EditBase, SimpleForm } from 'react-admin';
const PostEdit = () => {
const notify = useNotify();
const refresh = useRefresh();
const redirect = useRedirect();
const onSuccess = () => {
notify(`Changes saved`);
redirect('/posts');
refresh();
};
return (
...
);
}
```
{% endraw %}
The default `onSuccess` function is:
```js
() => {
notify('ra.notification.updated', {
messageArgs: { smart_count: 1 },
undoable: mutationMode === 'undoable'
});
redirect('list', resource, data.id, data);
}
```
**Tip**: If you just want to customize the redirect behavior, you can use [the `redirect` prop](#redirect) instead.
**Tip**: When you use `mutationMode="pessimistic"`, the `onSuccess` function receives the response from the `dataProvider.update()` call, which is the created/edited record (see [the dataProvider documentation for details](./DataProviderWriting.md#update)). You can use that response in the success side effects:
{% raw %}
```jsx
import * as React from 'react';
import { useNotify, useRefresh, useRedirect, EditBase, SimpleForm } from 'react-admin';
const PostEdit = () => {
const notify = useNotify();
const refresh = useRefresh();
const redirect = useRedirect();
const onSuccess = (data) => {
notify(`Changes to post "${data.title}" saved`);
redirect('/posts');
refresh();
};
return (
...
);
}
```
{% endraw %}
**Tip**: If you want to have different success side effects based on the button clicked by the user (e.g. if the creation form displays two submit buttons, one to "save and redirect to the list", and another to "save and display an empty form"), you can set the `mutationOptions` prop on [the `` component](./SaveButton.md), too.
Similarly, you can override the failure side effects with an `onError` option. By default, when the save action fails at the dataProvider level, react-admin shows a notification error.
{% raw %}
```jsx
import * as React from 'react';
import { useNotify, useRefresh, useRedirect, EditBase, SimpleForm } from 'react-admin';
const PostEdit = () => {
const notify = useNotify();
const refresh = useRefresh();
const redirect = useRedirect();
const onError = (error) => {
notify(`Could not edit post: ${error.message}`);
redirect('/posts');
refresh();
};
return (
...
);
}
```
{% endraw %}
The `onError` function receives the error from the `dataProvider.update()` call. It is a JavaScript Error object (see [the dataProvider documentation for details](./DataProviderWriting.md#error-format)).
The default `onError` function is:
```jsx
(error) => {
notify(typeof error === 'string' ? error : error.message || 'ra.notification.http_error', { type: 'error' });
if (mutationMode === 'undoable' || mutationMode === 'pessimistic') {
refresh();
}
}
```
**Tip**: If you want to have different failure side effects based on the button clicked by the user, you can set the `mutationOptions` prop on the `` component, too.
## `offline`
By default, `` renders nothing when there is no connectivity and the record hasn't been cached yet. You can provide your own component via the `offline` prop:
```jsx
import { EditBase } from 'react-admin';
export const PostEdit = () => (
No network. Could not load the post.}>
...
);
```
**Tip**: If the record is in the Tanstack Query cache but you want to warn the user that they may see an outdated version, you can use the `` component:
```jsx
import { EditBase, IsOffline } from 'react-admin';
export const PostEdit = () => (
No network. Could not load the post.}>
No network. The post data may be outdated.
...
);
```
## `queryOptions`
`` accepts a `queryOptions` prop to pass options to the react-query client.
This can be useful e.g. to override the default error side effect. By default, when the `dataProvider.getOne()` call fails at the dataProvider level, react-admin shows an error notification and refreshes the page.
You can override this behavior and pass custom side effects by providing a custom `queryOptions` prop:
```jsx
import * as React from 'react';
import { useNotify, useRefresh, useRedirect, EditBase, SimpleForm } from 'react-admin';
const PostEdit = () => {
const notify = useNotify();
const refresh = useRefresh();
const redirect = useRedirect();
const onError = (error) => {
notify(`Could not load post: ${error.message}`, { type: 'error' });
redirect('/posts');
refresh();
};
return (
...
);
}
```
The `onError` function receives the error from the dataProvider call (`dataProvider.getOne()`), which is a JavaScript Error object (see [the dataProvider documentation for details](./DataProviderWriting.md#error-format)).
The default `onError` function is:
```jsx
(error) => {
notify('ra.notification.item_doesnt_exist', { type: 'error' });
redirect('list', resource);
refresh();
}
```
## `redirectOnError`
By default, `` redirects to the list when an error happens while loading the record to edit. You can change the default redirection by setting the `redirectOnError` prop:
- `'list'`: redirect to the List view (the default)
- `false`: do not redirect
- A function `(resource, id) => string` to redirect to different targets depending on the record
```jsx
import { EditBase } from 'react-admin';
export const PostEdit = () => (
...
);
```
## `render`
Alternatively, you can pass a `render` function prop instead of children. This function will receive the `EditContext` as argument.
{% raw %}
```jsx
import { EditBase, Form, DateInput, ReferenceInput, SaveButton, TextInput } from 'react-admin';
const BookEdit = () => (
{
if (isPending) {
return Loading...
;
}
if (error) {
return (
{error.message}
);
}
return (
);
}}/>
);
```
{% endraw %}
## `resource`
By default, `` operates on the current `ResourceContext` (defined at the routing level), so under the `/posts/1/show` path, the `resource` prop will be `posts`. You may want to force a different resource. In this case, pass a custom `resource` prop, and it will override the `ResourceContext` value.
```jsx
import { EditBase } from 'react-admin';
export const UsersEdit = () => (
...
);
```
**Tip**: Pass both a custom `id` and a custom `resource` prop to use `` independently of the current URL. This even allows you to use more than one `` component in the same page.
## Security
The `` component requires authentication and will redirect anonymous users to the login page. If you want to allow anonymous access, use the [`disableAuthentication`](./Edit.md#disableauthentication) prop.
If your `authProvider` implements [Access Control](./Permissions.md#access-control), `` will only render if the user has the "edit" access to the related resource.
For instance, for the ``page below:
```tsx
import { EditBase, SimpleForm, TextInput } from 'react-admin';
// Resource name is "posts"
const PostEdit = () => (
);
```
`` will call `authProvider.canAccess()` using the following parameters:
```jsx
{ action: "edit", resource: "posts" }
```
Users without access will be redirected to the [Access Denied page](./Admin.md#accessdenied).
**Note**: Access control is disabled when you use [the `disableAuthentication` prop](./Edit.md#disableauthentication).