Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MUI Autocomplete doesnt take Value

I have an Autocomplete with all user in it.
When a user clicks on a Button on a different site it gets redirected to this site and i get the id of the user with a fetch.
Now i want that the User with the Corresponding ID is automaticly selected.
But when i try it nothing gets selected.

Example: https://codesandbox.io/s/frosty-water-tv27nn?file=/src/App.tsx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There is a warning in the console telling

MUI: A component is changing the uncontrolled value state of Autocomplete to be controlled.

That means your value changes from undefined to something else. And that prevents it from working correctly. Your initial value should be null, not undefined

  const [selectedUser, setSelectedUser] = useState(null);

Then it will work as expected

And also don’t update the state directly in the body of the component, it will push to that state on each render

userArray.push({ id: "s", fullName: "d" });  // never update state directly!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading