Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to omit basePath in Reference fields #6028

Merged
merged 2 commits into from
Mar 15, 2021
Merged

Conversation

fzaninotto
Copy link
Member

Problem

When using a Field component outside of a Datagrid or a Form, users have to inject props manually. They can understand that they must inject record and resource, but they often forget to inject basePath. It is not a problem, except for Reference fields, where the basePath is used to build the links to the related records. Using these Fields without a basePath leads to a cryptic error:

TypeError: Cannot read property 'replace' of undefined

image

The basePath property isn't well documented, and to be honest, it isn't very useful. In the most common case, the basePath equals /${resource}.

Solution

If the basePath isn't set, use the resource name instead. That way, users can omit the basePath when using ReferenceField, ReferenceArrayField, and ReferenceManyField manually.

    <ReferenceField
        resource="comments"
        record={data[id]}
        source="post_id"
        reference="posts"
-       basePath="/comments"
    >
        <TextField source="title" />
    </ReferenceField>

@fzaninotto fzaninotto added the RFR Ready For Review label Mar 12, 2021
@fzaninotto fzaninotto added this to the 3.14 milestone Mar 12, 2021
@djhi djhi merged commit 557f384 into next Mar 15, 2021
@djhi djhi deleted the optional-basepath branch March 15, 2021 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFR Ready For Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants