Skip to content

Commit

Permalink
[docs][Autocomplete] Require referentially stable value (#38734)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaldudak committed Sep 1, 2023
1 parent a71e6a0 commit bce191d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/data/material/components/autocomplete/autocomplete.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ Learn more about controlled and uncontrolled components in the [React documentat

{{"demo": "ControllableStates.js"}}

:::warning

If you control the `value`, make sure it's referentially stable between renders.
In other words, the reference to the value shouldn't change if the value itself doesn't change.

```tsx
// ⚠️ BAD
return <Autocomplete multiple value={allValues.filter((v) => v.selected)} />;

// 👍 GOOD
const selectedValues = React.useMemo(
() => allValues.filter((v) => v.selected),
[allValues],
);
return <Autocomplete multiple value={selectedValues} />;
```

In the first example, `allValues.filter` is called and returns **a new array** every render.
The fix includes memoizing the value, so it changes only when needed.
:::

## Free solo

Set `freeSolo` to true so the textbox can contain any arbitrary value.
Expand Down

0 comments on commit bce191d

Please sign in to comment.