Skip to content

[Autocomplete] A component is changing the default value state of an uncontrolled Autocomplete after being initialized with uncontrolled component #36467

@jmaldon1

Description

@jmaldon1

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

When using a basic Autocomplete setup like the following:

<Autocomplete
  defaultValue={initValue}
  options={options}
  freeSolo
  renderInput={(renderParams) => (
    <TextField
      {...renderParams}
    />
  )}
/>

Whenever the initValue changes, the value in the autocomplete does not update (which is correct behavior). But it does provide a warning in the console saying that I should be using a controlled component:

MUI: A component is changing the default value state of an uncontrolled Autocomplete after being initialized. To suppress this warning opt to use a controlled Autocomplete.

Expected behavior 🤔

In my situation, I do not want a controlled component, the reason the initValue is changing is because the user can update the field, submit the form, I will send that to the backend, the page will re-render and initValue will contain the updated value. defaultValue should ignore this change and not throw a warning at me, much like how a TextField works.

A workaround is to do something like this:

const [initValueState, initValueState] = useState(initValue);

return (<Autocomplete
  defaultValue={initValueState}
  options={options}
  freeSolo
  renderInput={(renderParams) => (
    <TextField
      {...renderParams}
    />
  )}
/>)

Context 🔦

No response

Your environment 🌎

npx @mui/envinfo
npmPackages:
    @emotion/react: ^11.10.5 => 11.10.5
    @emotion/styled: ^11.10.5 => 11.10.5
    @mui/base:  5.0.0-alpha.104
    @mui/core-downloads-tracker:  5.10.12
    @mui/icons-material: ^5.10.9 => 5.10.9
    @mui/material: ^5.10.12 => 5.10.12
    @mui/private-theming:  5.11.7
    @mui/styled-engine:  5.11.8
    @mui/system: ^5.11.8 => 5.11.8
    @mui/types:  7.2.3
    @mui/utils:  5.11.7
    @types/react: ^18.0.27 => 18.0.27
    react: ^18.2.0 => 18.2.0
    react-dom: ^18.2.0 => 18.2.0
    typescript: ^4.7.3 => 4.7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope: autocompleteChanges related to the autocomplete. This includes ComboBox.type: expected behaviorThe current behavior is already the one expected.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions