Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] Fix useGetOne section about query aggregation #7732

Merged
merged 1 commit into from
May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 77 additions & 7 deletions docs/useGetOne.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,98 @@ title: "useGetOne"

This hook calls `dataProvider.getOne()` when the component mounts. It queries the data provider for a single record, based on its `id`.

## Syntax

```jsx
// syntax
const { data, isLoading, error, refetch } = useGetOne(
resource,
{ id, meta },
options
);
```

The `meta` argument is optional. It can be anything you want to pass to the data provider, e.g. a list of fields to show in the result.

The `options` parameter is optional, and is passed to [react-query's `useQuery` hook](https://react-query.tanstack.com/reference/useQuery). It may contain the following options:

* `cacheTime`
* `enabled`
* `initialData`
* `initialDataUpdatedAt`
* `isDataEqual`
* `keepPreviousData`
* `meta`
* `notifyOnChangeProps`
* `notifyOnChangePropsExclusions`
* `onError`
* `onSettled`
* `onSuccess`
* `placeholderData`
* `queryKeyHashFn`
* `refetchInterval`
* `refetchIntervalInBackground`
* `refetchOnMount`
* `refetchOnReconnect`
* `refetchOnWindowFocus`
* `retry`
* `retryOnMount`
* `retryDelay`
* `select`
* `staleTime`
* `structuralSharing`
* `suspense`
* `useErrorBoundary`

Check [react-query's `useQuery` hook documentation](https://react-query.tanstack.com/reference/useQuery) for details on each of these options.

The react-query [query key](https://react-query.tanstack.com/guides/query-keys) for this hook is `[resource, 'getOne', { id: String(id), meta }]`.

// example
## Usage

Call `useGetOne` in a component to query the data provider for a single record, based on its `id`.

```jsx
import { useGetOne } from 'react-admin';

const UserProfile = ({ record }) => {
const { data, isLoading, error } = useGetOne('users', { id: record.id });
const { data: user, isLoading, error } = useGetOne('users', { id: record.userId });
if (isLoading) { return <Loading />; }
if (error) { return <p>ERROR</p>; }
return <div>User {data.username}</div>;
return <div>User {user.username}</div>;
};
```

## Aggregating `getOne` Calls

If you use `useGetOne` several times on a page for the same resource, replace the `useGetOne` call by `useGetManyAggregate`, as it de-duplicates and aggregates queries for a single record into one batch query for many records.

```diff
-import { useGetOne } from 'react-admin';
+import { useGetManyAggregate } from 'react-admin';

const UserProfile = ({ record }) => {
- const { data: user, isLoading, error } = useGetOne('users', { id: record.userId });
+ const { data: users, isLoading, error } = useGetManyAggregate('users', { ids: [record.userId] });
if (isLoading) { return <Loading />; }
if (error) { return <p>ERROR</p>; }
- return <div>User {user.username}</div>;
+ return <div>User {users[0].username}</div>;
};
```

**Tip**: If you use `useGetOne` several times on a page for the same resource, prefer [`useGetMany`](./useGetMany.md) instead, as it de-duplicates and aggregates queries for a single record into one batch query for many records.
This results in less calls to the dataProvider. For instance, if the `<UserProfile>` component above is rendered in a `<Datagrid>`, it will only make one call to `dataProvider.getMany()` for the entire list instead of one call to `dataProvider.getOne()` per row.

As this hook is often used to fetch references, react-admin exposes a `useReference` hook, which avoids doing the array conversion manually. It's an application hook rather than a data provider hook, so its syntax is a bit different. Prefer `useReference` to `useGetManyAggregate` when you use `useGetOne` to fetch a reference.

```diff
-useGetOne('posts', { id });
+useGetMany('posts', { id: [id] });
-import { useGetOne } from 'react-admin';
+import { useReference } from 'react-admin';

const UserProfile = ({ record }) => {
- const { data: user, isLoading, error } = useGetOne('users', { id: record.id });
+ const { referenceRecord: user, isLoading, error } = useReference({ reference: 'users', id: [record.id] });
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
if (isLoading) { return <Loading />; }
if (error) { return <p>ERROR</p>; }
return <div>User {data.username}</div>;
};
```