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

[BUG] - Maximum update depth exceeded on Autocomplete component #3094

Closed
stefnto opened this issue May 27, 2024 · 5 comments · Fixed by #3175
Closed

[BUG] - Maximum update depth exceeded on Autocomplete component #3094

stefnto opened this issue May 27, 2024 · 5 comments · Fixed by #3175
Assignees
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working

Comments

@stefnto
Copy link

stefnto commented May 27, 2024

NextUI Version

2.3.6

Describe the bug

I have an Autocomplete Component

     const [companyCountry, setCompanyCountry] = useState("");  
       <Autocomplete
          label={translations.country}
          labelPlacement="outside-left"
          placeholder={translations.country_placeholder}
          isRequired
          defaultItems={countries}
          selectedKey={companyCountry}
          // inputValue={companyCountry}
          onSelectionChange={setCompanyCountry}
          // onInputChange={setCompanyCountry}
          inputProps={{
            classNames: inputClasses
          }}
        >
          {(item) => <AutocompleteItem key={item.value}>{item.value}</AutocompleteItem>}
        </Autocomplete>

where countries is a list of objects with the key "value".
When an item is selected, if the dropdown menu is triggered from the arrow and all the items are displayed, if I select on a new item I get the error:
"Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops."
with Call Stack :
"onSelectionChange
node_modules@react-stately\combobox\dist\useComboBoxState.mjs (31:1)
onChangeCaller
node_modules@react-stately\utils\dist\useControlledState.mjs (29:1)
Object.eval [as setSelectedKey]
node_modules@react-stately\utils\dist\useControlledState.mjs (54:1)
setSelectedKey
node_modules@nextui-org\autocomplete\dist\chunk-SYFYBWAI.mjs (173:13)"
If I remove the {item.value} child in the AutocompleteItem, the error goes away, but the country items are not displayed neither on the input nor the dropdown list.

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Click on the arrow of autocomplete item to toggle the dropdown list when an item is already selected and select a new item

Expected behavior

I expected for just the new item value to be displayed

Screenshots or Videos

No response

Operating System Version

Windows

Browser

Chrome

Copy link

linear bot commented May 27, 2024

@CmCarti
Copy link

CmCarti commented May 29, 2024

I ran into this issue as well, same version. I found a workaround that isn't the worst, but hopefully sharing it will help with the debugging process:

This is what is currently not working, and throwing the depth exceeded error

const [selectedState, setSelectedState] = useState("");
return <Autocomplete selectedKey={selectedState} onSelectionChange={setSelectedState}>
      <AutocompleteItem key="" isReadOnly></AutocompleteItem>
      <AutocompleteItem key="AL">AL</AutocompleteItem>
      <AutocompleteItem key="AK">AK</AutocompleteItem>
      <AutocompleteItem key="AZ">AZ</AutocompleteItem>
      <AutocompleteItem key="AR">AR</AutocompleteItem>
      ...
</Autocomplete>

And if you instead check if the argument passed to onSelectionChange is truthy before setting state, it works fine:

const [selectedState, setSelectedState] = useState("");
return <Autocomplete selectedKey={selectedState} onSelectionChange={(e) => {
    if(e) {
      setSelectedState(e)
    }
}}>
      <AutocompleteItem key="" isReadOnly></AutocompleteItem>
      <AutocompleteItem key="AL">AL</AutocompleteItem>
      <AutocompleteItem key="AK">AK</AutocompleteItem>
      <AutocompleteItem key="AZ">AZ</AutocompleteItem>
      <AutocompleteItem key="AR">AR</AutocompleteItem>
      ...
</Autocomplete>

This could cause issues with clearing values, so another workaround would be to check if the typeof e is "string" before setting the value, then if e === null, set the value to "". I confirmed that this works as well.

@50Fifty
Copy link

50Fifty commented Jun 3, 2024

Can confirm that this is still an issue in NextUI 2.4.1

@TheUntraceable
Copy link

TheUntraceable commented Jun 3, 2024

I too am having this issue.
I have made a CodeSandbox with the bare minimum reproducible bug. First time ever using the platform so please let me know if there is anything wrong 😅
Edit TheUntraceable/next-app-template/main

@wingkwong
Copy link
Member

@TheUntraceable I don't see autocomplete component in your sandbox. Did you forget to save?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 Scope : Components Related to the components 🐛 Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants