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

Fix ReferenceArrayField passes empty data to child when loaded #6080

Merged
merged 1 commit into from
Mar 26, 2021

Conversation

fzaninotto
Copy link
Member

Problem

<ReferenceArrayField> relies on dataProvider.getMany() to retrieve the references, and to pass it them to its child (e.g. a <SingleFieldList>. When getMany() returns, for a brief moment, loaded is true and data is empty. If the child of <SingleFieldList> doesn't check for the existence of its injected record before looking up a field, it fails.

const WeakField = ({ record }: any) => <div>{record.title}</div>; // fails when record is undefined
...
<ReferenceArrayField
  record={{ id: 123, barIds: [1, 2] }}
  className="myClass"
  resource="foos"
  reference="bars"
  source="barIds"
  basePath="/foos"
>
  <SingleFieldList linkType={false}>
    <WeakField />
  </SingleFieldList>
</ReferenceArrayField>

Solution

The bug comes from a race condition in useReferenceArrayFieldController. This hook processes the data in a useEffect when loaded becomes true. As a consequence, the hook timeline is:

  1. returns { loaded: false, data: {} }
  2. dataProvider.getMany() returns
  3. returns { loaded: true, data: {} }
  4. Processes the useEffect
  5. returns { loaded: true, data: { 1: { id: 1, title: foo } }

At 3, the hook state is incoherent.

@fzaninotto fzaninotto added the RFR Ready For Review label Mar 25, 2021
@@ -203,4 +206,42 @@ describe('<ReferenceArrayField />', () => {
);
expect(container.getElementsByClassName('myClass')).toHaveLength(1);
});

it('should have defined data when loaded', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test fails without the fix

@djhi djhi added this to the 3.14 milestone Mar 26, 2021
@djhi djhi merged commit ee9afd6 into master Mar 26, 2021
@djhi djhi deleted the fix-ReferenceArrayField-loaded-data branch March 26, 2021 08:46
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