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:
218
+
219
+
```jsx
220
+
import { ListBase } from'ra-core';
221
+
import { Link } from'react-router';
222
+
223
+
constEmpty= () => (
224
+
<div>
225
+
<h4>
226
+
No product available
227
+
</h4>
228
+
<Link to="/products/create">Create the first product</Link>
229
+
</div>
230
+
);
231
+
232
+
constProductList= () => (
233
+
<ListBase empty={<Empty />}>
234
+
...
235
+
</ListBase>
236
+
);
237
+
```
238
+
239
+
The `empty` component can call the [`useListContext()`](./useListContext.md) hook to receive the same props as the `ListBase` child component.
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
+
213
299
## `error`
214
300
215
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