Skip to content

Commit e2b3d36

Browse files
committed
update data-fecthing (end)
1 parent 78725c9 commit e2b3d36

File tree

12 files changed

+141
-259
lines changed

12 files changed

+141
-259
lines changed

docs_headless/src/content/docs/data-fetching/useCreate.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
---
2-
layout: default
32
title: "useCreate"
4-
storybook_path: ra-core-dataprovider-usecreate-optimistic--success-case
53
---
64

7-
# `useCreate`
8-
95
This hook allows to call `dataProvider.create()` when the callback is executed.
106

117
## Syntax
@@ -34,7 +30,7 @@ So, should you pass the parameters when calling the hook, or when executing the
3430

3531
```tsx
3632
// set params when calling the hook
37-
import { useCreate, useRecordContext } from 'react-admin';
33+
import { useCreate, useRecordContext } from 'ra-core';
3834

3935
const LikeButton = () => {
4036
const record = useRecordContext();
@@ -48,7 +44,7 @@ const LikeButton = () => {
4844
};
4945

5046
// set params when calling the create callback
51-
import { useCreate, useRecordContext } from 'react-admin';
47+
import { useCreate, useRecordContext } from 'ra-core';
5248

5349
const LikeButton = () => {
5450
const record = useRecordContext();
@@ -97,6 +93,8 @@ const LikeButton = () => {
9793
- `returnPromise`.
9894

9995
```tsx
96+
import { useNotify, useRedirect } from 'ra-core';
97+
10098
const notify = useNotify();
10199
const redirect = useRedirect();
102100

@@ -128,14 +126,13 @@ Additional options are passed to [React Query](https://tanstack.com/query/v5/)'s
128126

129127
Check [the useMutation documentation](https://tanstack.com/query/v5/docs/react/reference/useMutation) for a detailed description of all options.
130128

131-
**Tip**: In react-admin components that use `useCreate`, you can override the mutation options using the `mutationOptions` prop. This is very common when using mutation hooks like `useCreate`, e.g., to display a notification or redirect to another page.
129+
**Tip**: In ra-core components that use `useCreate`, you can override the mutation options using the `mutationOptions` prop. This is very common when using mutation hooks like `useCreate`, e.g., to display a notification or redirect to another page.
132130

133-
For instance, here is a button using `<Create mutationOptions>` to notify the user of success using the bottom notification banner:
131+
For instance, here is a button using `<CreateBase mutationOptions>` to notify the user of success using the bottom notification banner:
134132

135-
{% raw %}
136133
```tsx
137134
import * as React from 'react';
138-
import { useNotify, useRedirect, Create, SimpleForm } from 'react-admin';
135+
import { useNotify, useRedirect, CreateBase, Form } from 'ra-core';
139136

140137
const PostCreate = () => {
141138
const notify = useNotify();
@@ -147,15 +144,14 @@ const PostCreate = () => {
147144
};
148145

149146
return (
150-
<Create mutationOptions={{ onSuccess }}>
151-
<SimpleForm>
147+
<CreateBase mutationOptions={{ onSuccess }}>
148+
<Form>
152149
...
153-
</SimpleForm>
154-
</Create>
150+
</Form>
151+
</CreateBase>
155152
);
156153
}
157154
```
158-
{% endraw %}
159155

160156
## Return Value
161157

@@ -240,6 +236,8 @@ See [Optimistic Rendering and Undo](./Actions.md#optimistic-rendering-and-undo)
240236
The `onError` callback is called when the mutation fails. It's the perfect place to display an error message to the user.
241237

242238
```jsx
239+
import { useCreate, useNotify } from 'ra-core';
240+
243241
const notify = useNotify();
244242
const [create, { data, isPending, error }] = useCreate(
245243
'comments',
@@ -259,6 +257,8 @@ const [create, { data, isPending, error }] = useCreate(
259257
The `onSettled` callback is called at the end of the mutation, whether it succeeds or fails. It will receive either the `data` or the `error`.
260258

261259
```jsx
260+
import { useCreate, useNotify } from 'ra-core';
261+
262262
const notify = useNotify();
263263
const [create, { data, isPending, error }] = useCreate(
264264
'comments',
@@ -278,6 +278,8 @@ const [create, { data, isPending, error }] = useCreate(
278278
The `onSuccess` callback is called when the mutation succeeds. It's the perfect place to display a notification or to redirect the user to another page.
279279

280280
```jsx
281+
import { useCreate, useNotify, useRedirect } from 'ra-core';
282+
281283
const notify = useNotify();
282284
const redirect = useRedirect();
283285
const [create, { data, isPending, error }] = useCreate(
@@ -292,7 +294,7 @@ const [create, { data, isPending, error }] = useCreate(
292294
);
293295
```
294296

295-
In `pessimistic` mutation mode, `onSuccess` executes *after* the `dataProvider.create()` responds. React-admin passes the result of the `dataProvider.create()` call as the first argument to the `onSuccess` callback.
297+
In `pessimistic` mutation mode, `onSuccess` executes *after* the `dataProvider.create()` responds. Ra-core passes the result of the `dataProvider.create()` call as the first argument to the `onSuccess` callback.
296298

297299
In `optimistic` mutation mode, `onSuccess` executes *before* the `dataProvider.create()` is called, without waiting for the response. The callback receives no argument.
298300

docs_headless/src/content/docs/data-fetching/useDataProvider.md

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
---
2-
layout: default
32
title: "useDataProvider"
4-
storybook_path: ra-core-dataprovider-usedataprovider--prefetching
53
---
64

7-
# `useDataProvider`
8-
9-
React-admin stores the `dataProvider` object in a React context, so it's available from anywhere in your application code. The `useDataProvider` hook exposes the Data Provider to let you call it directly.
5+
Ra-core stores the `dataProvider` object in a React context, so it's available from anywhere in your application code. The `useDataProvider` hook exposes the Data Provider to let you call it directly.
106

117
## Syntax
128

@@ -34,7 +30,7 @@ Here is how to query the Data Provider for the current user profile:
3430

3531
```jsx
3632
import { useState, useEffect } from 'react';
37-
import { useDataProvider } from 'react-admin';
33+
import { useDataProvider } from 'ra-core';
3834
import { Loading, Error } from './MyComponents';
3935

4036
const UserProfile = ({ userId }) => {
@@ -94,15 +90,15 @@ const dataProvider = {
9490
It is necessary to use `useDataProvider` in conjunction with React Query's `useMutation` to call this method when the user clicks on a button:
9591

9692
```jsx
97-
import { useDataProvider, Button } from 'react-admin';
93+
import { useDataProvider } from 'ra-core';
9894
import { useMutation } from '@tanstack/react-query';
9995

10096
const BanUserButton = ({ userId }) => {
10197
const dataProvider = useDataProvider();
10298
const { mutate, isPending } = useMutation({
10399
mutationFn: () => dataProvider.banUser(userId)
104100
});
105-
return <Button label="Ban" onClick={() => mutate()} disabled={isPending} />;
101+
return <button onClick={() => mutate()} disabled={isPending}>Ban</button>;
106102
};
107103
```
108104

@@ -112,7 +108,7 @@ The `useDataProvider` hook accepts a generic parameter for the `dataProvider` ty
112108

113109
```tsx
114110
// In src/dataProvider.ts
115-
import { DataProvider } from 'react-admin';
111+
import { DataProvider } from 'ra-core';
116112

117113
export interface DataProviderWithCustomMethods extends DataProvider {
118114
archive: (resource: string, params: {
@@ -128,24 +124,22 @@ export const dataProvider: DataProviderWithCustomMethods = {
128124
}
129125

130126
// In src/ArchiveButton.tsx
131-
import { Button, useDataProvider } from 'react-admin';
132-
import ArchiveIcon from '@mui/icons-material/Archive';
127+
import { useDataProvider, useRecordContext } from 'ra-core';
133128
import { DataProviderWithCustomMethods } from './src/dataProvider';
134129

135130
export const ArchiveButton = () => {
136131
const dataProvider = useDataProvider<DataProviderWithCustomMethods>();
137-
const record = useRecord();
132+
const record = useRecordContext();
138133

139134
return (
140-
<Button
141-
label="Archive"
135+
<button
142136
onClick={() => {
143137
// TypeScript knows the archive method
144138
dataProvider.archive('resource', { id: record.id })
145139
}}
146140
>
147-
<ArchiveIcon />
148-
</Button>
141+
📁 Archive
142+
</button>
149143
);
150144
};
151145
```

docs_headless/src/content/docs/data-fetching/useDelete.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
---
2-
layout: default
32
title: "useDelete"
4-
storybook_path: ra-core-dataprovider-usedelete-optimistic--success-case
53
---
64

7-
# `useDelete`
8-
95
This hook allows calling `dataProvider.delete()` when the callback is executed and deleting a single record based on its `id`.
106

117
## Syntax
@@ -34,7 +30,7 @@ So, should you pass the parameters when calling the hook, or when executing the
3430

3531
```jsx
3632
// set params when calling the hook
37-
import { useDelete, useRecordContext } from 'react-admin';
33+
import { useDelete, useRecordContext } from 'ra-core';
3834

3935
const DeleteButton = () => {
4036
const record = useRecordContext();
@@ -50,7 +46,7 @@ const DeleteButton = () => {
5046
};
5147

5248
// set params when calling the deleteOne callback
53-
import { useDelete, useRecordContext } from 'react-admin';
49+
import { useDelete, useRecordContext } from 'ra-core';
5450

5551
const DeleteButton = () => {
5652
const record = useRecordContext();

docs_headless/src/content/docs/data-fetching/useDeleteMany.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
---
2-
layout: default
32
title: "useDeleteMany"
43
---
54

6-
# `useDeleteMany`
7-
85
This hook allows to call `dataProvider.deleteMany()` when the callback is executed, and delete an array of records based on their `ids`.
96

107
## Syntax
@@ -33,7 +30,7 @@ So, should you pass the parameters when calling the hook, or when executing the
3330

3431
```jsx
3532
// set params when calling the hook
36-
import { useListContext, useDeleteMany } from 'react-admin';
33+
import { useListContext, useDeleteMany } from 'ra-core';
3734

3835
const BulkDeletePostsButton = () => {
3936
const { selectedIds } = useListContext();
@@ -49,7 +46,7 @@ const BulkDeletePostsButton = () => {
4946
};
5047

5148
// set params when calling the deleteMany callback
52-
import { useListContext, useDeleteMany } from 'react-admin';
49+
import { useListContext, useDeleteMany } from 'ra-core';
5350

5451
const BulkDeletePostsButton = () => {
5552
const { selectedIds } = useListContext();

0 commit comments

Comments
 (0)