You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
217
218
218
-
{% raw %}
219
219
```jsx
220
220
import { ListBase } from'ra-core';
221
221
import { Link } from'react-router';
@@ -235,10 +235,67 @@ const ProductList = () => (
235
235
</ListBase>
236
236
);
237
237
```
238
-
{% endraw %}
239
238
240
239
The `empty` component can call the [`useListContext()`](./useListContext.md) hook to receive the same props as the `ListBase` child component.
241
240
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:
<i>{book.title}</i>, by {book.author} ({book.year})
257
+
</li>
258
+
))}
259
+
</ul>
260
+
);
261
+
}
262
+
263
+
constBookList= () => (
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
+
constSimpleBookList= () => {
274
+
const { data, isPending } =useListContext();
275
+
if (isPending) returnnull;
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
+
constBookList= () => (
292
+
-<ListBase>
293
+
+<ListBase emptyWhileLoading>
294
+
<SimpleBookList />
295
+
</ListBase>
296
+
);
297
+
```
298
+
242
299
## `error`
243
300
244
301
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