Skip to content

Commit 224c44f

Browse files
committed
Improve documentation
1 parent b311942 commit 224c44f

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

docs_headless/src/content/docs/InfiniteListBase.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ That's enough to display a basic list with infinite scroll functionality. When u
106106
| `debounce` | Optional | `number` | `500` | The debounce delay in milliseconds to apply when users change the sort or filter parameters. |
107107
| `disable Authentication` | Optional | `boolean` | `false` | Set to `true` to disable the authentication check. |
108108
| `disable SyncWithLocation` | Optional | `boolean` | `false` | Set to `true` to disable the synchronization of the list parameters with the URL. |
109+
| `empty` | Optional | `ReactNode` | - | The component to display when the list is empty. |
110+
| `emptyWhileLoading` | Optional | `boolean` | - | Set to `true` to return `null` while the list is loading. |
109111
| `error` | Optional | `ReactNode` | - | The component to render when failing to load the list of records. |
110112
| `exporter` | Optional | `function` | - | The function to call to export the list. |
111113
| `filter` | Optional | `object` | - | The permanent filter values. |

docs_headless/src/content/docs/ListBase.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The `<ListBase>` component accepts the following props:
8787
| `disableAuthentication` | Optional | `boolean` | `false` | Set to `true` to disable the authentication check. |
8888
| `disableSyncWithLocation` | Optional | `boolean` | `false` | Set to `true` to disable the synchronization of the list parameters with the URL. |
8989
| `empty` | Optional | `ReactNode` | - | The component to display when the list is empty. |
90+
| `emptyWhileLoading` | Optional | `boolean` | - | Set to `true` to return `null` while the list is loading. |
9091
| `error` | Optional | `ReactNode` | - | The component to render when failing to load the list of records. |
9192
| `exporter` | Optional | `function` | - | The function to call to export the list. |
9293
| `filter` | Optional | `object` | - | The permanent filter values. |
@@ -215,7 +216,6 @@ const Dashboard = () => (
215216
216217
By default, `<ListBase>` renders the children when there is no result and no active filter. If you want for instance to invite users to create the first record, you can render a custom component via the `empty` prop:
217218
218-
{% raw %}
219219
```jsx
220220
import { ListBase } from 'ra-core';
221221
import { Link } from 'react-router';
@@ -235,10 +235,67 @@ const ProductList = () => (
235235
</ListBase>
236236
);
237237
```
238-
{% endraw %}
239238
240239
The `empty` component can call the [`useListContext()`](./useListContext.md) hook to receive the same props as the `ListBase` child component.
241240
241+
## `emptyWhileLoading`
242+
243+
Many list view components return null when the data is loading. If you use a custom view component as the `<ListBase>` children instead, you'll have to handle the case where the `data` is not yet defined.
244+
245+
That means that the following will fail on load with a "ReferenceError: data is not defined" error:
246+
247+
```jsx
248+
import { ListBase, useListContext } from 'ra-core';
249+
250+
const SimpleBookList = () => {
251+
const { data } = useListContext();
252+
return (
253+
<ul>
254+
{data.map(book => (
255+
<li key={book.id}>
256+
<i>{book.title}</i>, by {book.author} ({book.year})
257+
</li>
258+
))}
259+
</ul>
260+
);
261+
}
262+
263+
const BookList = () => (
264+
<ListBase>
265+
<SimpleBookList />
266+
</ListBase>
267+
);
268+
```
269+
270+
You can handle this case by getting the `isPending` variable from the [`useListContext`](./useListContext.md) hook:
271+
272+
```jsx
273+
const SimpleBookList = () => {
274+
const { data, isPending } = useListContext();
275+
if (isPending) return null;
276+
return (
277+
<ul>
278+
{data.map(book => (
279+
<li key={book.id}>
280+
<i>{book.title}</i>, by {book.author} ({book.year})
281+
</li>
282+
))}
283+
</ul>
284+
);
285+
}
286+
```
287+
288+
The `<ListBase emptyWhileLoading>` prop provides a convenient shortcut for that use case. When enabled, `<ListBase>` won't render its child until `data` is defined.
289+
290+
```diff
291+
const BookList = () => (
292+
- <ListBase>
293+
+ <ListBase emptyWhileLoading>
294+
<SimpleBookList />
295+
</ListBase>
296+
);
297+
```
298+
242299
## `error`
243300
244301
By default, `<ListBase>` renders the children when an error happens while loading the list of records. You can render an error component via the `error` prop:

0 commit comments

Comments
 (0)