I'm trying to implement AutoComplete component of Material UI in React but i'm getting some errors, can't understand what am I doing wrong.
Those are the errors I get:
MUI: The value provided to Autocomplete is invalid.
None of the options match with `{"id":"2","name":"Stefanie","description":"I love her"}`.
You can use the `isOptionEqualToValue` prop to customize the equality test.
This is my code:
function WomanAutoComplete(props) {
const data = [
{
id: '1',
name: 'Stefanie',
description: 'I love her',
},
{
id: '2',
name: 'Jasmin',
description: 'I miss her',
},
{
id: '3',
name: 'Angie',
description: 'I hate her',
}
];
const defaultValue = { id: '1' };
const [value, setValue] = useState(defaultValue);
const defaultProps = {
options: data,
getOptionLabel: (option) => option.name,
};
return (
<Autocomplete
{...defaultProps}
id="grid-choose-woman"
clearOnEscape
defaultValue={defaultValue}
onChange={(event, newValue) => {
console.log(newValue);
setValue(newValue);
}}
renderInput={(params) => (
<TextField {...params} label="Woman" />
)}
/>
);
}
I would not use the default prop if you want to put value use the options={} props to inject the values in to the ac component.
< Autocomplete
options={data}
getOptionLabel={(option) => option.name}
id = "grid-choose-pesticide"
clearOnEscape
defaultValue = {
defaultValue
}
onChange = {
(event, newValue) => {
console.log(newValue);
setValue(newValue);
}
}
renderInput = {
(params) => ( <
TextField { ...params
}
label = "Pesticide" / >
)
}
/>