Skip to content

Commit

Permalink
Merge pull request #6498 from marmelab/fix-linktorecord-doc
Browse files Browse the repository at this point in the history
[Doc] Fix Syntax Error in linkToRecord code snippet
  • Loading branch information
fzaninotto authored Aug 12, 2021
2 parents b0b99a9 + f55c317 commit 8219644
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions docs/Fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -1611,19 +1611,20 @@ const UserShow = props => (

And now you can use a regular Field component, and the label displays correctly in the Show view.

### Linking to other records
### Linking To Other Records

Your custom Field component might need to display a link to another record. React Admin provides a `linkToRecord(basePath, id[, linkType])` method for this purpose.
A custom Field component might need to display a link to another record. React Admin provides a `linkToRecord(basePath, id[, linkType])` method for this purpose.

```js
import { linkToRecord, useRecordContext } from 'react-admin';
import { linkToRecord, useRecordContext, useGetOne } from 'react-admin';
import { Link } from 'react-router-dom';

const MyCustomField = () => {
const AuthorField = () => {
const post = useRecordContext(props);
const linkToUser = linkToRecord('/users', post.user_id, 'show');
const { data, loaded } = useGetOne('users', post.user_id);
const userShowPage = linkToRecord('/users', post.user_id, 'show');

return <Link to={linkToUser}>{seller.username}</Link>;
return loaded ? <Link to={userShowPage}>{data.username}</Link> : null;
};
```

Expand Down

0 comments on commit 8219644

Please sign in to comment.