Material-ui <Autocomplete /> getOptionLabel - passing empty string as value
Asked Answered
G

9

35

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

Gateway answered 16/7, 2020 at 11:21 Comment(2)
From what I saw that getOptionLabel function is first called with [""] which will return undefined in the ternary. Why is the default state an array instead of an empty string?Eichhorn
i have this wrong behavior by wrong value optionTardiff
C
35

I got the same error. And, the following worked for me.

getOptionLabel={(option) => option.name || ""}
Colby answered 27/8, 2021 at 2:54 Comment(0)
T
5

I solved this problem by just setting values like this.

getOptionLabel={(option)=>(option.name?option.name:'')}
Townsend answered 23/10, 2021 at 8:36 Comment(0)
M
4

I faced a similar problem and I had options as an array of strings. Eg: ["delhi", "new york", "mumbai"]. I fixed it by adding a check, if the option is a string, if not use an empty string.

getOptionLabel={(option) => typeof option === 'string'
                  || option instanceof String ? option : ""}
Mitchmitchael answered 6/9, 2021 at 5:22 Comment(0)
S
2

value={props.state || null} this worked for me in material UI version 4

Shifty answered 9/8, 2022 at 6:19 Comment(0)
E
1

Your default value is an array with an empty string [""] which when evaluated in the ternary will return true, thus trying to access the property option.name that is undefined. You need either to test for that or initialise the state with an empty string which is a falsy value in JS.

Eichhorn answered 16/7, 2020 at 11:58 Comment(0)
U
1

Because the option may be your object or a string. So that it don't have property. Hope this code below can help you.

getOptionLabel={ (option: Object | string) => option['name'] || option }
Ulm answered 12/10, 2022 at 7:27 Comment(0)
E
1

I solved this by adding:

getOptionLabel={(option) =>typeof(option) =='number'? String (option) :option}

In my case, option is an array of strings and numbers.

Engineman answered 1/5, 2023 at 10:32 Comment(0)
M
1

In my case none of answers helped. I used (option as Object).toString()

getOptionLabel={(option) => (typeof option === 'string' ? option : (option as Object).toString()}

And this error only occures for Autocomplete with multiple={false}

MUI 5.13.3

Merridie answered 11/8, 2023 at 17:7 Comment(0)
C
0

I used null to set value in useState:

const [selectedState, setSelectedState] = useState(null)
Cockahoop answered 8/11, 2023 at 15:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.