@@ -54,7 +54,7 @@ export const UserView = () => {
5454 if (context .error ) {
5555 return <p className = " error" >{ context .error .toString ()} </p >;
5656 }
57-
57+
5858 return <p >{ value } </p >;
5959};
6060
@@ -85,7 +85,7 @@ You can pass any component of your own as child, to render the related records a
8585You can access the list context using the ` useReferenceFieldContext ` hook.
8686
8787``` tsx
88- import { Show , SimpleShowLayout , ReferenceField , TextField , DateField } from ' react-admin' ;
88+ import { ReferenceFieldBase } from ' react-admin' ;
8989
9090export const UserView = () => {
9191 const { isPending, error } = useReferenceFieldContext ();
@@ -111,14 +111,14 @@ export const MyReferenceField = () => (
111111
112112## ` render `
113113
114- Alternatively, you can pass a ` render ` function prop instead of children. This function will receive the ` ReferenceFieldContext ` as argument.
114+ Alternatively, you can pass a ` render ` function prop instead of children. This function will receive the ` ReferenceFieldContext ` as argument.
115115
116116``` jsx
117117export const MyReferenceField = () => (
118118 < ReferenceFieldBase
119119 source= " user_id"
120120 reference= " users"
121- render= {({ error, isPending }) => {
121+ render= {({ error, isPending, referenceRecord }) => {
122122 if (isPending) {
123123 return < p> Loading... < / p> ;
124124 }
@@ -130,7 +130,7 @@ export const MyReferenceField = () => (
130130 < / p>
131131 );
132132 }
133- return < p> {value }< / p> ;
133+ return < p> {referenceRecord . name }< / p> ;
134134 }}
135135 / >
136136);
@@ -143,7 +143,9 @@ The `render` function prop will take priority on `children` props if both are se
143143` <ReferenceFieldBase> ` can display a custom message when the referenced record is missing, thanks to the ` empty ` prop.
144144
145145``` jsx
146- < ReferenceFieldBase source= " user_id" reference= " users" empty= " Missing user" / >
146+ < ReferenceFieldBase source= " user_id" reference= " users" empty= " Missing user" >
147+ ...
148+ < / ReferenceFieldBase>
147149```
148150
149151` <ReferenceFieldBase> ` renders the ` empty ` element when:
@@ -154,23 +156,31 @@ The `render` function prop will take priority on `children` props if both are se
154156You can pass either a React element or a string to the ` empty ` prop:
155157
156158``` jsx
157- < ReferenceField source= " user_id" reference= " users" empty= {< span> Missing user< / span> } / >
158- < ReferenceField source= " user_id" reference= " users" empty= " Missing user" / >
159+ < ReferenceFieldBase source= " user_id" reference= " users" empty= {< span> Missing user< / span> } >
160+ ...
161+ < / ReferenceFieldBase>
162+ < ReferenceFieldBase source= " user_id" reference= " users" empty= " Missing user" >
163+ ...
164+ < / ReferenceFieldBase>
159165```
160166
161167## ` link `
162168
163169To change the link from the ` <Edit> ` page to the ` <Show> ` page, set the ` link ` prop to "show".
164170
165171``` jsx
166- < ReferenceFieldBase source= " user_id" reference= " users" link= " show" / >
172+ < ReferenceFieldBase source= " user_id" reference= " users" link= " show" >
173+ ...
174+ < / ReferenceFieldBase>
167175```
168176
169177You can also prevent ` <ReferenceFieldBase> ` from adding a link to children by setting ` link ` to ` false ` .
170178
171179``` jsx
172180// No link
173- < ReferenceFieldBase source= " user_id" reference= " users" link= {false } / >
181+ < ReferenceFieldBase source= " user_id" reference= " users" link= {false } >
182+ ...
183+ < / ReferenceFieldBase>
174184```
175185
176186You can also use a custom ` link ` function to get a custom path for the children. This function must accept ` record ` and ` reference ` as arguments.
@@ -181,7 +191,9 @@ You can also use a custom `link` function to get a custom path for the children.
181191 source= " user_id"
182192 reference= " users"
183193 link= {(record , reference ) => ` /my/path/to/${ reference} /${ record .id } ` }
184- / >
194+ >
195+ ...
196+ < / ReferenceFieldBase>
185197```
186198## ` queryOptions `
187199
@@ -195,8 +207,9 @@ For instance, to pass [a custom `meta`](./Actions.md#meta-parameter):
195207 source= " user_id"
196208 reference= " users"
197209 queryOptions= {{ meta: { foo: ' bar' } }}
210+ render= {({ referenceRecord }) => referenceRecord .name }
198211>
199- < TextField source = " name " / >
212+ ...
200213< / ReferenceFieldBase>
201214```
202215{% endraw %}
@@ -208,41 +221,44 @@ The resource to fetch for the related record.
208221For instance, if the ` posts ` resource has a ` user_id ` field, set the ` reference ` to ` users ` to fetch the user related to each post.
209222
210223``` jsx
211- < ReferenceFieldBase source= " user_id" reference= " users" / >
224+ < ReferenceFieldBase source= " user_id" reference= " users" >
225+ ...
226+ < / ReferenceFieldBase>
212227```
213228## ` sortBy `
214229
215230By default, when used in a ` <Datagrid> ` , and when the user clicks on the column header of a ` <ReferenceFieldBase> ` , react-admin sorts the list by the field ` source ` . To specify another field name to sort by, set the ` sortBy ` prop.
216231
217232``` jsx
218- < ReferenceFieldBase source= " user_id" reference= " users" sortBy= " user.name" / >
233+ < ReferenceFieldBase source= " user_id" reference= " users" sortBy= " user.name" >
234+ ...
235+ < / ReferenceFieldBase>
219236```
220237## Performance
221238
222239<iframe src =" https://www.youtube-nocookie.com/embed/egBhWqF3sWc " title =" YouTube video player " frameborder =" 0 " allow =" accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share " allowfullscreen style =" aspect-ratio : 16 / 9 ;width :100% ;margin-bottom :1em ;" ></iframe >
223240
224- When used in a ` <DataTable> ` , ` <ReferenceFieldBase> ` fetches the referenced record only once for the entire table.
225-
226- ![ ReferenceField] ( ./img/reference-field.png )
241+ When used in a ` <DataTable> ` , ` <ReferenceFieldBase> ` fetches the referenced record only once for the entire table.
227242
228243For instance, with this code:
229244
230245``` jsx
231- import { List , DataTable , ReferenceField , EditButton } from ' react-admin' ;
246+ import { ListBase , ListIterator , ReferenceFieldBase } from ' react-admin' ;
232247
233248export const PostList = () => (
234- < List>
235- < DataTable>
236- < DataTable .Col source= " id" / >
237- < DataTable .Col label= " User" source= " user_id" >
238- < ReferenceField source= " user_id" reference= " users" / >
239- < / DataTable .Col >
240- < DataTable .Col source= " title" / >
241- < DataTable .Col >
242- < EditButton / >
243- < / DataTable .Col >
244- < / DataTable>
245- < / List>
249+ < ListBase>
250+ < ListIterator
251+ render= {({ referenceRecord}) => (
252+ < div>
253+ < p> #{referenceRecord? .id }< / p>
254+ < ReferenceFieldBase source= " user_id" reference= " users" >
255+ < AuthorView / >
256+ < / ReferenceFieldBase>
257+ < p> {referenceRecord .title }< / p>
258+ < / div>
259+ )}
260+ / >
261+ < / ListBase>
246262);
247263` ` `
248264
@@ -279,15 +295,18 @@ For example, the following code prefetches the authors referenced by the posts:
279295{% raw %}
280296` ` ` jsx
281297const PostList = () => (
282- < List queryOptions= {{ meta: { prefetch: [' author' ] } }}>
283- < DataTable>
284- < DataTable .Col source= " title" / >
285- < DataTable .Col source= " author_id" >
286- {/** renders without an additional request */ }
287- < ReferenceFieldBase source= " author_id" reference= " authors" / >
288- < / DataTable .Col >
289- < / DataTable>
290- < / List>
298+ < ListBase queryOptions= {{ meta: { prefetch: [' author' ] } }}>
299+ < ListIterator
300+ render= {({ title, author_id }) => (
301+ < div>
302+ < h3> {title}< / h3>
303+ < ReferenceFieldBase source= " author_id" reference= " authors" >
304+ < AuthorView / >
305+ < / ReferenceFieldBase>
306+ < / div>
307+ )}
308+ / >
309+ < / ListBase>
291310);
292311` ` `
293312{% endraw %}
0 commit comments