Skip to content
Advertisement

Material UI Autocomplete add item with multiple select

I tried out the Autocomplete example from Material UI. Its about an add option when entering a new value. Here is the link to the demo: https://codesandbox.io/s/material-demo-forked-lgeju?file=/demo.js What I am wondering about is that the value in the input isnt actually added to the options in this example so it doesnt actually work. Why is that so? Also I cant use it for multiple select, (via adding the property “multiple” to the Autocomplete) since this would produce a “TypeError Cannot read property ‘length’ of null”. Is there a way to make that work for multiple select?

Would appreciate any help a lot.

Advertisement

Answer

According to the documentation regarding multiple prop:

If true, value must be an array and the menu will support multiple selections.

So just initialize your state as an array so that it would have the property length and is an array so that satisfies the condition I quoted above

const [value, setValue] = React.useState([]);
Advertisement