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
`<WithListContext>` executes its `render` function using the current `ListContext` as parameter. It's the render prop version of [the `useListContext` hook](./useListContext.md).
10
7
11
8
Use it to render a list of records already fetched.
@@ -14,32 +11,34 @@ Use it to render a list of records already fetched.
14
11
15
12
The most common use case for `<WithListContext>` is to build a custom list view on-the-fly, without creating a new component, in a place where records are available inside a `ListContext`.
16
13
17
-
For instance, a list of book tags fetched via [`<ReferenceArrayField>`](./ReferenceArrayField.md):
14
+
For instance, a list of book tags fetched via [`<ReferenceArrayFieldBase>`](../fields/ReferenceArrayFieldBase.md):
@@ -141,22 +143,19 @@ As a reminder, the [`ListContext`](./useListContext.md) is an object with the fo
141
143
142
144
## Availability
143
145
144
-
Whenever you use a react-admin component to fetch a list of records, react-admin stores the data in a [`ListContext`](./useListContext.md). Consequently, `<WithListContext>` works in any component that is a descendant of:
146
+
Whenever you use a ra-core component to fetch a list of records, ra-core stores the data in a [`ListContext`](./useListContext.md). Consequently, `<WithListContext>` works in any component that is a descendant of:
145
147
146
-
- the [`<List>`](./ListBase.md), [`<InfiniteList>`](./InfiniteList.md), and [`<ListBase>`](./ListBase.md) components
147
-
- the [`<ArrayField>`](./ArrayField.md) component
148
-
- the [`<ReferenceManyField>`](./ReferenceManyField.md) component
149
-
- the [`<ReferenceArrayField>`](./ReferenceArrayField.md) component
148
+
- the [`<ListBase>`](./ListBase.md) component
149
+
- the [`<ReferenceArrayFieldBase>`](../fields/ReferenceArrayFieldBase.md) component
150
150
151
151
## Building a Chart
152
152
153
153
A common use case is to build a chart based on the list data. For instance, the following component fetches a list of fruit prices (using `<ListBase>`), and draws a line chart with the data using [Echarts](https://echarts.apache.org/en/index.html):
154
154
155
155

Another use case is to create a button that refreshes the current list. As the [`ListContext`](./useListContext.md) exposes the `refetch` function, it's as simple as:
Copy file name to clipboardExpand all lines: docs_headless/src/content/docs/list/useList.md
+11-12Lines changed: 11 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,10 @@
1
1
---
2
-
layout: default
3
2
title: "useList"
4
3
---
5
4
6
-
# `useList`
7
-
8
5
The `useList` hook allows to create a `ListContext` based on local data. `useList` creates callbacks for sorting, paginating, filtering, and selecting records from an array.
9
6
10
-
Thanks to it, you can display your data inside a [`<DataTable>`](./DataTable.md), a [`<SimpleList>`](./SimpleList.md) or an [`<EditableDatagrid>`](./EditableDatagrid.md).
7
+
Thanks to it, you can display your data inside your own list components.
11
8
12
9
## Usage
13
10
@@ -17,8 +14,8 @@ Thanks to it, you can display your data inside a [`<DataTable>`](./DataTable.md)
17
14
import {
18
15
useList,
19
16
ListContextProvider,
20
-
DataTable,
21
-
} from'react-admin';
17
+
} from'ra-core';
18
+
import { DataTable} from'./DataTable';
22
19
23
20
constdata= [
24
21
{ id:1, name:'Arnold' },
@@ -42,7 +39,8 @@ const MyComponent = () => {
42
39
If you use it with data coming from the `dataProvider`, don't forget to pass the `isPending` prop so that it only manipulates the data once it's available:
Copy file name to clipboardExpand all lines: docs_headless/src/content/docs/list/useListContext.md
+29-46Lines changed: 29 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,56 +1,47 @@
1
1
---
2
-
layout: default
3
2
title: "useListContext"
4
3
---
5
4
6
-
# `useListContext`
7
-
8
-
Whenever react-admin displays a List, it creates a `ListContext` to store the list data, as well as filters, pagination, sort state, and callbacks to update them.
5
+
Whenever ra-core displays a List, it creates a `ListContext` to store the list data, as well as filters, pagination, sort state, and callbacks to update them.
9
6
10
7
The `ListContext` is available to descendants of:
11
8
12
-
-`<List>`,
13
-
-`<ListGuesser>`,
14
9
-`<ListBase>`,
15
-
-`<ReferenceArrayField>`,
16
-
-`<ReferenceManyField>`
10
+
-`<ReferenceArrayFieldBase>`,
11
+
-`<ReferenceManyFieldBase>`
17
12
18
-
All descendant components can therefore access the list context, using the `useListContext` hook. As a matter of fact, react-admin's `<DataTable>`, `<FilterForm>`, and `<Pagination>` components all use the `useListContext` hook.
13
+
All descendant components can therefore access the list context, using the `useListContext` hook.
19
14
20
15
## Usage
21
16
22
-
Call `useListContext` in a component, then use this component as a descendant of a `List` component.
17
+
Call `useListContext` in a component, then use this component as a descendant of a list component.
23
18
24
19
```jsx
25
20
// in src/posts/Aside.js
26
-
import { Typography } from'@mui/material';
27
-
import { useListContext } from'react-admin';
21
+
import { useListContext } from'ra-core';
28
22
29
23
exportconstAside= () => {
30
24
const { data, isPending } =useListContext();
31
25
if (isPending) returnnull;
32
26
return (
33
27
<div>
34
-
<Typography variant="h6">Posts stats</Typography>
35
-
<Typography variant="body2">
28
+
<h6>Posts stats</h6>
29
+
<p>
36
30
Total views: {data.reduce((sum, post) => sum +post.views, 0)}
37
-
</Typography>
31
+
</p>
38
32
</div>
39
33
);
40
34
};
41
35
42
36
// in src/posts/PostList.js
43
-
import { List, DataTable} from'react-admin';
37
+
import { ListBase} from'ra-core';
44
38
importAsidefrom'./Aside';
45
39
46
40
exportconstPostList= () => (
47
-
<List aside={<Aside />}>
48
-
<DataTable>
49
-
<DataTable.Col source="id"/>
50
-
<DataTable.Col source="title"/>
51
-
<DataTable.Col source="views"/>
52
-
</DataTable>
53
-
</List>
41
+
<ListBase>
42
+
<Aside />
43
+
{/* My list content */}
44
+
</ListBase>
54
45
);
55
46
```
56
47
@@ -100,17 +91,16 @@ const {
100
91
`useListContext` often forces you to create a new component just to access the list context. If you prefer a declarative approach based on render props, you can use [the `<WithListContext>` component](./WithListContext.md) instead:
101
92
102
93
```jsx
103
-
import { WithListContext } from'react-admin';
104
-
import { Typography } from'@mui/material';
94
+
import { WithListContext } from'ra-core';
105
95
106
96
exportconstAside= () => (
107
97
<WithListContext render={({ data, isPending }) =>
108
98
!isPending && (
109
99
<div>
110
-
<Typography variant="h6">Posts stats</Typography>
111
-
<Typography variant="body2">
100
+
<h6>Posts stats</h6>
101
+
<p>
112
102
Total views: {data.reduce((sum, post) => sum +post.views, 0)}
113
-
</Typography>
103
+
</p>
114
104
</div>
115
105
)} />
116
106
);
@@ -128,7 +118,7 @@ You can use it to update the filters in a custom filter component:
{/* TypeScript knows that posts is of type Post[] */}
182
171
Total views: {posts.reduce((sum, post) =>sum+post.views, 0)}
183
-
</Typography>
172
+
</p>
184
173
</div>
185
174
);
186
175
};
187
176
188
177
exportconst PostList = () => (
189
-
<Listaside={<Aside />}>
190
-
<DataTable>
191
-
<DataTable.Colsource="id" />
192
-
<DataTable.Colsource="title" />
193
-
<DataTable.Colsource="views" />
194
-
</DataTable>
195
-
</List>
178
+
<ListBase>
179
+
<Aside />
180
+
{/* My list content */}
181
+
</ListBase>
196
182
);
197
183
```
198
184
199
185
## Recipes
200
186
201
187
You can find many usage examples of `useListContext` in the documentation, including:
202
188
203
-
-[Adding a Side Component with `<List actions>`](./List.md#aside)
204
-
-[Building a Custom Actions Bar via `<List actions>`](./List.md#actions)
205
-
-[Building a Custom Empty Page via `<List empty>`](./List.md#empty)
206
189
-[Building a Custom Filter](./FilteringTutorial.md#building-a-custom-filter)
207
190
-[Building a Custom Sort Control](./ListTutorial.md#building-a-custom-sort-control)
208
191
-[Building a Custom Pagination Control](./ListTutorial.md#building-a-custom-pagination)
209
-
-[Building a Custom Iterator](./ListTutorial.md#building-a-custom-iterator)
192
+
-[Building a Custom List Layout](./ListTutorial.md#building-a-custom-list-layout)
210
193
211
-
**Tip**: [`<ReferenceManyField>`](./ReferenceManyField.md), as well as other relationship-related components, also implement a `ListContext`. That means you can use a `<DataTable>` of a `<Pagination>` inside these components!
194
+
**Tip**: [`<ReferenceManyFieldBase>`](../fields/ReferenceManyFieldBase.md), as well as other relationship-related components, also implement a `ListContext`. That means you can use custom list components inside these components!
0 commit comments