I am using material-ui autocomplete. I am passing to its property options some array of states. The problem I face is with getOptionLabel:
Material-UI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for [""].
I have 2 components. The child one is:
const StateSelect = (props) => {
const classes = useStyles();
const handlePick = (e, v) => {
props.setState(v);
};
return (
<Autocomplete
className={classes.inputStyle}
options={states}
getOptionLabel={(option) => (option ? option.name : "")}
onChange={handlePick}
value={props.state}
renderInput={(params) => (
<TextField {...params} label="State" variant="outlined" />
)}
/>
);
};
And in the parent one I invoke this child component:
<StateSelect
state={selectedState}
setState={(state) => setSelectedState(state)}
/>
In the parent one I have the React hook that controls value of the StateSelect:
const [selectedState, setSelectedState] = useState([""]);
So when I initially pass selectedState as prop to StateSelect it is [''] and I am getting this error message. How could I pass empty value as initial and not to get this error?
I uploaded simple version of my code:
https://codesandbox.io/s/smoosh-field-j2o1p?file=/src/inputStates/input.js
[""]
which will return undefined in the ternary. Why is the default state an array instead of an empty string? – Eichhorn