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

Infinite re-rendering using disabled AutocompleteArrayInput in a ReferenceArrayInput #9714

Closed
Nitix opened this issue Mar 12, 2024 · 6 comments · Fixed by #9717
Closed

Infinite re-rendering using disabled AutocompleteArrayInput in a ReferenceArrayInput #9714

Nitix opened this issue Mar 12, 2024 · 6 comments · Fixed by #9717
Assignees
Labels

Comments

@Nitix
Copy link
Contributor

Nitix commented Mar 12, 2024

What you were expecting:

An stable page with a An AutocompleteArrayInput in an ReferenceArrayInput

What happened instead:

AutocompleteArrayInput keeps re-rendering

Steps to reproduce:

Add a disabled AutocompleteArrayInput in a ReferenceArrayInput

Related code:
https://stackblitz.com/edit/github-dmhu7h?file=src%2Fposts%2FPostEdit.tsx,package.json

Other information:

Environment

  • React-admin version: 4.6.12
  • Last version that did not exhibit the issue (if applicable):
  • React version: 18
  • Browser: Firefox 123
  • Stack trace (in case of a JS error):
Warning: Maximum update depth exceeded. This can happen when a component calls setState inside useEffect, but useEffect either doesn't have a dependency array, or one of the dependencies changes on every render.
AutocompleteInput@https://githubdmhu7h-miy1--8080--810981ba.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-MFCMXL24.js?v=75640c3d:113882:21
AutocompleteArrayInput@https://githubdmhu7h-miy1--8080--810981ba.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-MFCMXL24.js?v=75640c3d:114219:22
ChoicesContextProvider@https://githubdmhu7h-miy1--8080--810981ba.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-MFCMXL24.js?v=75640c3d:15661:18
ResourceContextProvider@https://githubdmhu7h-miy1--8080--810981ba.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-MFCMXL24.js?v=75640c3d:14501:18
ReferenceArrayInput@https://githubdmhu7h-miy1--8080--810981ba.local-corp.webcontainer.io/node_modules/.vite/deps/chunk-MFCMXL24.js?v=75640c3d:119879:14
div

My poor Firefox 🦊
image

@adguernier
Copy link
Contributor

adguernier commented Mar 12, 2024

Reproduced, thanks for this report 🙏

@adguernier adguernier added the bug label Mar 12, 2024
@fzaninotto
Copy link
Member

fzaninotto commented Mar 12, 2024

Interesting: I can reproduce with 2 ReferenceArrayInputs

const PostEdit = () => (
  <Edit>
    <SimpleForm defaultValues={{ average_note: 0 }} warnWhenUnsavedChanges>
      <ReferenceArrayInput source="authors" reference="users">
        <AutocompleteArrayInput fullWidth disabled />
      </ReferenceArrayInput>
      <TagReferenceInput reference="tags" source="tags" label="Tags" />
    </SimpleForm>
  </Edit>
);

But not with 1:

const PostEdit = () => (
  <Edit>
    <SimpleForm defaultValues={{ average_note: 0 }} warnWhenUnsavedChanges>
      <ReferenceArrayInput source="authors" reference="users">
        <AutocompleteArrayInput fullWidth disabled />
      </ReferenceArrayInput>
      {/*<TagReferenceInput reference="tags" source="tags" label="Tags" /> */}
    </SimpleForm>
  </Edit>
);

See my stackblitz https://stackblitz.com/edit/github-dmhu7h-ytpyvk?file=src%2Fposts%2FPostEdit.tsx

@fzaninotto
Copy link
Member

I confirm the bug is triggered by having 2 ReferenceArrayInput in the same form, one of which at least uses a disabled AutocompleteArrayInput. This is probably a loop in the form state management. The trigger: React-hook-form removes disabled fields from the input state.

@fzaninotto
Copy link
Member

No need for ReferenceArrayInput. Two autocompleteInputs are enough.

const PostEdit = () => (
  <Edit>
    <SimpleForm>
        <AutocompleteArrayInput source="authors" disabled choices={[]} />
        <AutocompleteArrayInput source="tag_ids" choices={[]} />
    </SimpleForm>
  </Edit>
);

Click on the second input to trigger the bug

@fzaninotto
Copy link
Member

@fzaninotto
Copy link
Member

The bug occurs here in react-admin:

resetField(source, { defaultValue });

Commenting this line removed the bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants