@@ -12,13 +12,13 @@ Use it to render a list of records already fetched.
12
12
13
13
## Usage
14
14
15
- 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 ` .
15
+ 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
16
17
17
For instance, a list of book tags fetched via [ ` <ReferenceArrayField> ` ] ( ./ReferenceArrayField.md ) :
18
18
19
19
``` jsx
20
20
import { List , DataTable , ReferenceArrayField , WithListContext } from ' react-admin' ;
21
- import { Chip , Stack } from ' @mui/material' ;
21
+ import { Chip , Stack , Typography } from ' @mui/material' ;
22
22
23
23
const BookList = () => (
24
24
< List>
@@ -27,15 +27,18 @@ const BookList = () => (
27
27
< DataTable .Col source= " title" / >
28
28
< DataTable .Col source= " tag_ids" label= " Tags" >
29
29
< ReferenceArrayField reference= " tags" source= " tag_ids" >
30
- < WithListContext render= {({ isPending, data }) => (
31
- ! isPending && (
30
+ < WithListContext
31
+ loading= {< Typography> Loading tags... < / Typography> }
32
+ errorElement= {< Typography> Error while loading tags< / Typography> }
33
+ empty= {< Typography> No associated tags< / Typography> }
34
+ render= {({ data }) => (
32
35
< Stack direction= " row" spacing= {1 }>
33
36
{data .map (tag => (
34
37
< Chip key= {tag .id } label= {tag .name } / >
35
38
))}
36
39
< / Stack>
37
- )
38
- )} / >
40
+ )}
41
+ / >
39
42
< / ReferenceArrayField>
40
43
< / DataTable .Col >
41
44
< / DataTable>
@@ -45,10 +48,11 @@ const BookList = () => (
45
48
46
49
![ List of tags] ( ./img/reference-array-field.png )
47
50
48
- The equivalent with ` useListContext ` would require an intermediate component:
51
+ The equivalent with ` useListContext ` would require an intermediate component, manually handling the loading, error, and empty states :
49
52
50
53
``` jsx
51
54
import { List , DataTable , ReferenceArrayField , WithListContext } from ' react-admin' ;
55
+ import { Chip , Stack , Typography } from ' @mui/material' ;
52
56
53
57
const BookList = () => (
54
58
< List>
@@ -65,31 +69,71 @@ const BookList = () => (
65
69
);
66
70
67
71
const TagList = () => {
68
- const { isPending , data } = useListContext ();
69
- return isPending
70
- ? null
71
- : (
72
- < Stack direction= " row" spacing= {1 }>
73
- {data .map (tag => (
74
- < Chip key= {tag .id } label= {tag .name } / >
75
- ))}
76
- < / Stack>
77
- );
72
+ const { isPending , error , data , total } = useListContext ();
73
+
74
+ if (isPending) {
75
+ return < Typography> Loading tags... < / Typography> ;
76
+ }
77
+
78
+ if (error) {
79
+ return < Typography> Error while loading tags< / Typography> ;
80
+ }
81
+
82
+ if (data == null || data .length === 0 || total === 0 ) {
83
+ return < Typography> No associated tags< / Typography> ;
84
+ }
85
+
86
+ return (
87
+ < Stack direction= " row" spacing= {1 }>
88
+ {data .map (tag => (
89
+ < Chip key= {tag .id } label= {tag .name } / >
90
+ ))}
91
+ < / Stack>
92
+ );
78
93
};
79
94
```
80
95
81
96
Whether you use ` <WithListContext> ` or ` useListContext ` is a matter of coding style.
82
97
98
+ ## Standalone usage
99
+
100
+ You can also use ` <WithListContext> ` outside of a ` ListContext ` by filling ` data ` , ` total ` , ` error ` , and ` isPending ` properties manually.
101
+
102
+ ``` jsx
103
+ import { WithListContext } from ' react-admin' ;
104
+ import { Chip , Stack , Typography } from ' @mui/material' ;
105
+
106
+ const TagList = ({data, isPending}) => (
107
+ < WithListContext
108
+ data= {data}
109
+ isPending= {isPending}
110
+ loading= {< Typography> Loading tags... < / Typography> }
111
+ empty= {< Typography> No associated tags< / Typography> }
112
+ render= {({ data }) => (
113
+ < Stack direction= " row" spacing= {1 }>
114
+ {data .map (tag => (
115
+ < Chip key= {tag .id } label= {tag .name } / >
116
+ ))}
117
+ < / Stack>
118
+ )}
119
+ / >
120
+ );
121
+ ```
122
+
83
123
## Props
84
124
85
125
` <WithListContext> ` accepts a single ` render ` prop, which should be a function.
86
126
87
- | Prop | Required | Type | Default | Description |
88
- | -----------| ----------| ----------------| ---------| ---------------------------------------------------------|
89
- | ` empty ` | Optional | ` ReactElement ` | | The component to display when the data is empty. |
90
- | ` error ` | Optional | ` ReactElement ` | | The component to display in case of error. |
91
- | ` loading ` | Optional | ` ReactElement ` | | The component to display while checking authorizations. |
92
- | ` render ` | Required | ` function ` | | The function to render the data |
127
+ | Prop | Required | Type | Default | Description |
128
+ | ----------------| ----------| ----------------| ---------| -----------------------------------------------------------|
129
+ | ` data ` | Optional | ` RecordType[] ` | | The list data in standalone usage. |
130
+ | ` empty ` | Optional | ` ReactNode ` | | The component to display when the data is empty. |
131
+ | ` error ` | Optional | ` Error ` | | The error in standalone usage. |
132
+ | ` errorElement ` | Optional | ` ReactNode ` | | The component to display in case of error. |
133
+ | ` isPending ` | Optional | ` boolean ` | | Determine if the list is loading in standalone usage. |
134
+ | ` loading ` | Optional | ` ReactNode ` | | The component to display while checking authorizations. |
135
+ | ` render ` | Required | ` function ` | | The function to render the data |
136
+ | ` total ` | Optional | ` number ` | | The total number of data in the list in standalone usage. |
93
137
94
138
## ` empty `
95
139
@@ -114,26 +158,26 @@ If `empty` is not provided, the render function will be called with empty data.
114
158
/ >
115
159
```
116
160
117
- ## ` error `
161
+ ## ` errorElement `
118
162
119
- Use ` error ` to display a message when an error is thrown.
163
+ Use ` errorElement ` to display a message when an error is thrown.
120
164
121
- If ` error ` is not provided, the render function will be called with the error.
165
+ If ` errorElement ` is not provided, the render function will be called with the error.
122
166
123
167
``` jsx
124
168
< WithListContext
125
- error = {< p> Error while loading books... < / p> }
126
- render= {({ data }) => (
127
- < ul>
128
- {data .map (book => (
129
- < li key= {book .id }>
130
- < i> {book .title }< / i> , published on
131
- {book .published_at }
132
- < / li>
133
- ))}
134
- < / ul>
135
- )}
136
- loading= {< p> Loading... < / p> }
169
+ errorElement = {< p> Error while loading books... < / p> }
170
+ render= {({ data }) => (
171
+ < ul>
172
+ {data .map (book => (
173
+ < li key= {book .id }>
174
+ < i> {book .title }< / i> , published on
175
+ {book .published_at }
176
+ < / li>
177
+ ))}
178
+ < / ul>
179
+ )}
180
+ loading= {< p> Loading... < / p> }
137
181
/ >
138
182
```
139
183
0 commit comments