|
| 1 | +--- |
| 2 | +name: react-admin |
| 3 | +description: This skill should be used when building, modifying, or debugging a react-admin application — including creating resources, lists, forms, data fetching, authentication, relationships between entities, custom pages, or any CRUD admin interface built with react-admin. |
| 4 | +--- |
| 5 | + |
| 6 | +# React-Admin Development Guide |
| 7 | + |
| 8 | +React-admin is a framework for building single-page applications on top of REST/GraphQL APIs. It builds on top of React Query, react-hook-form, react-router, and Material UI. It provides 150+ components and dozens of hooks. Before writing custom code, always check if react-admin already provides a component or hook for the task. Full documentation: https://marmelab.com/react-admin/doc/ |
| 9 | + |
| 10 | +## Providers (Backend Abstraction) |
| 11 | + |
| 12 | +React-admin never calls APIs directly. All communication goes through **providers** — adapters that translate react-admin's standardized calls into API-specific requests. The three main providers are: |
| 13 | + |
| 14 | +- **dataProvider**: All CRUD operations (`getList`, `getOne`, `create`, `update`, `delete`, `getMany`, `getManyReference`, `updateMany`, `deleteMany`). See [DataProviders](https://marmelab.com/react-admin/DataProviders.html) and [50+ existing adapters](https://marmelab.com/react-admin/DataProviderList.html). |
| 15 | +- **authProvider**: Authentication and authorization. See [Authentication](https://marmelab.com/react-admin/Authentication.html). |
| 16 | +- **i18nProvider**: Translations (`translate`, `changeLocale`, `getLocale`). |
| 17 | + |
| 18 | +**Critical rule**: Never use `fetch`, `axios`, or direct HTTP calls in components. Always use data provider hooks. This ensures proper caching, loading states, error handling, authentication, and optimistic rendering. |
| 19 | + |
| 20 | +## Composition (Not God Components) |
| 21 | + |
| 22 | +React-admin uses composition over configuration. Override behavior by passing child components, not by setting dozens of props: |
| 23 | + |
| 24 | +```jsx |
| 25 | +<Edit actions={<MyCustomActions />}> |
| 26 | + <SimpleForm> |
| 27 | + <TextInput source="title" /> |
| 28 | + </SimpleForm> |
| 29 | +</Edit> |
| 30 | +``` |
| 31 | + |
| 32 | +To customize the layout, pass a custom layout component to `<Admin layout={MyLayout}>`. To customize the menu, pass it to `<Layout menu={MyMenu}>`. This chaining is by design — see [Architecture](https://marmelab.com/react-admin/Architecture.html). |
| 33 | + |
| 34 | +## Context: Pull, Don't Push |
| 35 | + |
| 36 | +React-admin components expose data to descendants via React contexts. Access data using hooks rather than passing props down: |
| 37 | + |
| 38 | +- `useRecordContext()` — current record in Show/Edit/Create views. See [useRecordContext](https://marmelab.com/react-admin/useRecordContext.html). |
| 39 | +- `useListContext()` — list data, filters, pagination, sort in List views. See [useListContext](https://marmelab.com/react-admin/useListContext.html). |
| 40 | +- `useShowContext()`, `useEditContext()`, `useCreateContext()` — page-level state for detail views. |
| 41 | +- `useTranslate()` — translation function from i18nProvider. |
| 42 | +- `useGetIdentity()` — current user from authProvider. |
| 43 | + |
| 44 | +## Hooks Over Custom Components |
| 45 | + |
| 46 | +When a react-admin component's UI doesn't fit, use the underlying hook instead of building from scratch. Controller hooks (named `use*Controller`) provide all the logic without the UI: |
| 47 | + |
| 48 | +- `useListController()` — list fetching, filtering, pagination logic |
| 49 | +- `useEditController()` — edit form fetching and submission logic |
| 50 | +- `useShowController()` — show page data fetching logic |
| 51 | + |
| 52 | +## Routing |
| 53 | + |
| 54 | +`<Resource>` declares CRUD routes automatically (`/posts`, `/posts/create`, `/posts/:id/edit`, `/posts/:id/show`). Use `<CustomRoutes>` for non-CRUD pages. Use `useCreatePath()` to build resource URLs and `<Link>` from react-admin for navigation. Default router is react-router (HashRouter), but TanStack Router is also supported via `routerProvider`. See [Routing](https://marmelab.com/react-admin/Routing.html). |
| 55 | + |
| 56 | +## Data Fetching |
| 57 | + |
| 58 | +### Query Hooks (Reading Data) |
| 59 | + |
| 60 | +```jsx |
| 61 | +const { data, total, isPending, error } = useGetList('posts', { |
| 62 | + pagination: { page: 1, perPage: 25 }, |
| 63 | + sort: { field: 'created_at', order: 'DESC' }, |
| 64 | + filter: { status: 'published' }, |
| 65 | +}); |
| 66 | + |
| 67 | +const { data: record, isPending } = useGetOne('posts', { id: 123 }); |
| 68 | +const { data: records } = useGetMany('posts', { ids: [1, 2, 3] }); |
| 69 | +const { data, total } = useGetManyReference('comments', { |
| 70 | + target: 'post_id', id: 123, |
| 71 | + pagination: { page: 1, perPage: 25 }, |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +See [useGetList](https://marmelab.com/react-admin/useGetList.html), [useGetOne](https://marmelab.com/react-admin/useGetOne.html). |
| 76 | + |
| 77 | +### Mutation Hooks (Writing Data) |
| 78 | + |
| 79 | +All mutations return `[mutate, state]`. They support three **mutation modes**: |
| 80 | + |
| 81 | +- **pessimistic** (default): Wait for server response, then update UI. |
| 82 | +- **optimistic**: Update UI immediately, revert on server error. |
| 83 | +- **undoable**: Update UI, show undo notification, commit after delay. |
| 84 | + |
| 85 | +```jsx |
| 86 | +const [create, { isPending }] = useCreate(); |
| 87 | +const [update] = useUpdate(); |
| 88 | +const [deleteOne] = useDelete(); |
| 89 | + |
| 90 | +// Call with resource and params |
| 91 | +create('posts', { data: { title: 'Hello' } }); |
| 92 | +update('posts', { id: 1, data: { title: 'Updated' }, previousData: record }); |
| 93 | +deleteOne('posts', { id: 1, previousData: record }); |
| 94 | +``` |
| 95 | + |
| 96 | +Pass `mutationMode: 'optimistic'` or `'undoable'` for instant UI feedback. See [useCreate](https://marmelab.com/react-admin/useCreate.html), [useUpdate](https://marmelab.com/react-admin/useUpdate.html). |
| 97 | + |
| 98 | +## Authentication & Authorization |
| 99 | + |
| 100 | +```typescript |
| 101 | +const authProvider = { |
| 102 | + login: ({ username, password }) => Promise<void>, |
| 103 | + logout: () => Promise<void>, |
| 104 | + checkAuth: () => Promise<void>, // Verify credentials are valid |
| 105 | + checkError: (error) => Promise<void>, // Detect auth errors from API responses |
| 106 | + getIdentity: () => Promise<{ id, fullName, avatar }>, |
| 107 | + getPermissions: () => Promise<any>, |
| 108 | + canAccess: ({ resource, action, record }) => Promise<boolean>, // RBAC |
| 109 | +}; |
| 110 | +``` |
| 111 | + |
| 112 | +Each auth provider method has a corresponding hook (e.g. `useGetIdentity()`, `useCanAccess()`). |
| 113 | + |
| 114 | +- **Custom routes are public by default.** Wrap them with `<Authenticated>` or call `useAuthenticated()` to require login. See [Authenticated](https://marmelab.com/react-admin/Authenticated.html). |
| 115 | +- Centralize authorization in `authProvider.canAccess()`, not in individual components. Use `useCanAccess()` to check permissions. See [useCanAccess](https://marmelab.com/react-admin/useCanAccess.html) and [AuthRBAC](https://marmelab.com/react-admin/AuthRBAC.html). |
| 116 | +- The dataProvider must include credentials (Bearer token, cookies) in requests — authProvider handles login, but dataProvider handles API calls. Configure `httpClient` in data provider setup. |
| 117 | + |
| 118 | +## Relationships Between Entities |
| 119 | + |
| 120 | +Fetching all the data (including relationships) upfront for a given page is an anti-pattern. Instead, fetch related records on demand using reference fields and inputs. |
| 121 | + |
| 122 | +### Displaying Related Records (Fields) |
| 123 | + |
| 124 | +```jsx |
| 125 | +{/* Show a the company of the current record based on its company_id */} |
| 126 | +<ReferenceField source="company_id" reference="companies" /> |
| 127 | + |
| 128 | +{/* Show a list of related records (reverse FK) */} |
| 129 | +<ReferenceManyField reference="comments" target="post_id"> |
| 130 | + <DataTable> |
| 131 | + <TextField source="body" /> |
| 132 | + <DateField source="created_at" /> |
| 133 | + </DataTable> |
| 134 | +</ReferenceManyField> |
| 135 | + |
| 136 | +{/* Show multiple referenced records (array of IDs) */} |
| 137 | +<ReferenceArrayField source="tag_ids" reference="tags"> |
| 138 | + <SingleFieldList> |
| 139 | + <ChipField source="name" /> |
| 140 | + </SingleFieldList> |
| 141 | +</ReferenceArrayField> |
| 142 | +``` |
| 143 | + |
| 144 | +See [ReferenceField](https://marmelab.com/react-admin/ReferenceField.html), [ReferenceManyField](https://marmelab.com/react-admin/ReferenceManyField.html), [ReferenceArrayField](https://marmelab.com/react-admin/ReferenceArrayField.html). |
| 145 | + |
| 146 | +### Editing Related Records (Inputs) |
| 147 | + |
| 148 | +```jsx |
| 149 | +{/* Select from another resource (FK) */} |
| 150 | +<ReferenceInput source="company_id" reference="companies" /> |
| 151 | + |
| 152 | +{/* Multi-select from another resource (array of IDs) */} |
| 153 | +<ReferenceArrayInput source="tag_ids" reference="tags" /> |
| 154 | +``` |
| 155 | + |
| 156 | +See [ReferenceInput](https://marmelab.com/react-admin/ReferenceInput.html), [ReferenceArrayInput](https://marmelab.com/react-admin/ReferenceArrayInput.html). |
| 157 | + |
| 158 | +## Forms |
| 159 | + |
| 160 | +React-admin forms are built on react-hook-form. Use `<SimpleForm>` for single-column layouts and `<TabbedForm>` for multi-tab layouts. See [SimpleForm](https://marmelab.com/react-admin/SimpleForm.html), [TabbedForm](https://marmelab.com/react-admin/TabbedForm.html). |
| 161 | + |
| 162 | +Pass validators to input components: `required()`, `minLength(min)`, `maxLength(max)`, `minValue(min)`, `maxValue(max)`, `number()`, `email()`, `regex(pattern, message)`, or a custom function returning an error string. |
| 163 | + |
| 164 | +```jsx |
| 165 | +<TextInput source="title" validate={[required(), minLength(3)]} /> |
| 166 | +``` |
| 167 | + |
| 168 | +Use RHF's `useWatch()` to create dynamic forms that react to field values: |
| 169 | + |
| 170 | +## Resource Definition |
| 171 | + |
| 172 | +Encapsulate resource components in index files for clean imports: |
| 173 | + |
| 174 | +```jsx |
| 175 | +// posts/index.ts |
| 176 | +export default { |
| 177 | + list: PostList, |
| 178 | + create: PostCreate, |
| 179 | + edit: PostEdit, |
| 180 | + icon: PostIcon, |
| 181 | + recordRepresentation: (record) => record.title, // How records appear in references |
| 182 | +}; |
| 183 | +``` |
| 184 | + |
| 185 | +See [Resource](https://marmelab.com/react-admin/Resource.html), [RecordRepresentation](https://marmelab.com/react-admin/RecordRepresentation.html). |
| 186 | + |
| 187 | +## Custom Data Provider Methods |
| 188 | + |
| 189 | +Extend the dataProvider with domain-specific methods: |
| 190 | + |
| 191 | +```jsx |
| 192 | +const dataProvider = { |
| 193 | + ...baseDataProvider, |
| 194 | + archivePost: async (id) => { /* custom logic */ }, |
| 195 | +}; |
| 196 | +// Call via useDataProvider and useQuery: |
| 197 | +// const dp = useDataProvider(); |
| 198 | +// const { data } = useQuery(['archivePost', id], () => dp.archivePost(id)); |
| 199 | +``` |
| 200 | + |
| 201 | +## Persistent Client State (Store) |
| 202 | + |
| 203 | +Use `useStore()` for persistent user preferences (theme, column visibility, saved filters): |
| 204 | + |
| 205 | +```jsx |
| 206 | +const [theme, setTheme] = useStore('theme', 'light'); |
| 207 | +``` |
| 208 | + |
| 209 | +See [Store](https://marmelab.com/react-admin/Store.html). |
| 210 | + |
| 211 | +## Notification, Redirect, Refresh |
| 212 | + |
| 213 | +```jsx |
| 214 | +const notify = useNotify(); |
| 215 | +const redirect = useRedirect(); |
| 216 | +const refresh = useRefresh(); |
| 217 | + |
| 218 | +notify('Record saved', { type: 'success' }); |
| 219 | +redirect('list', 'posts'); // Navigate to /posts |
| 220 | +redirect('edit', 'posts', 123); // Navigate to /posts/123 |
| 221 | +refresh(); // Invalidate all queries |
| 222 | +``` |
| 223 | + |
| 224 | +## Deprecations |
| 225 | + |
| 226 | +- Use DataTable instead of Datagrid |
| 227 | +- Prefer `<CanAccess>` and `useCanAccess` for authorization checks |
0 commit comments