[Autocomplete] getOptionLabel returns undefined for key that exists
See original GitHub issue- The issue is present in the latest release.
- I have searched the issues of this repository and believe that this is not a duplicate.
Current Behavior 😯
I am getting this error -> The getOptionLabel
method of Autocomplete returned undefined instead of a string for “Simple Moving Average”.
Expected Behavior 🤔
The value for “Simple Moving Average” should be displayed in the AutoComplete as it exists in the list.
Steps to Reproduce 🕹
Consider this example in codesandbox -> https://jcphp.csb.app/
Steps:
- Open the code sandbox example
- Open developer tools
- When the component mounts, you will see a bunch of errors like
"The
getOptionLabel
method of Autocomplete returned undefined instead of a string for “Simple Moving Average” - When the component is being mounted onInputChange is also being triggered because of the value not being found in the list and hence it set’s it to blank string
Context 🔦
The use case is to render autocomplete component while mapping over a list. So if a list consists of 10 elements, 10 autocomplete components would be visible where each of the autocomplete input value is mapped to one of the object’s key in the list.
Your Environment 🌎
“@material-ui/lab”: “4.0.0-alpha.56” “react”: “^16.13.1” Chrome : Version 84.0.4147.135
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:15 (1 by maintainers)
Top Results From Across the Web
[Autocomplete] getOptionLabel returns undefined for key that ...
I am getting this error -> The getOptionLabel method of Autocomplete returned undefined instead of a string for "Simple Moving Average".
Read more >Material-ui <Autocomplete /> getOptionLabel - passing empty ...
Material-UI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for [""]. I have 2 components. The child one is:
Read more >Demystifying Errors in MUI Autocomplete — part - Medium
MUI: The `getOptionLabel` method of Autocomplete returned undefined instead of a string for “Andorra”. So the rule no:2 is simple — just ...
Read more >Autocomplete API - Material UI - MUI
Name Type Default
options * array
renderInput * func
autoComplete bool false
Read more >[Solved]-How to get the option id instead of the shown string in ...
useState(null); const handleAutocomplete = (event, newValue) => { if (newValue != null) ... return ( <Autocomplete options={options} style={{ width: '80%', ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi guys, it seems like more people are facing the same issue. Here is what I do to get rid of the error. I’m using this code sandbox as a demo: https://codesandbox.io/s/jcphp
In
Demo.js
line 155.Just change
getOptionLabel={(option) => option.indicatorName}
to
getOptionLabel={(option) => option.indicatorName ? option.indicatorName : ""}
This worked for me because it will return a string no matter if the option exists or not. I hope this helps.
@afrieirham No I am not, I have figured out a way to handle it, I am using the Autocomplete component a bit differently now.