MUI Autocomplete doesnt take Value

Advertisements

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

>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!

Leave a ReplyCancel reply