question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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:

  1. Open the code sandbox example
  2. Open developer tools
  3. 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”
  4. 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

32reactions
afrieirhamcommented, May 6, 2021

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.

28reactions
nihalwasheretoocommented, Dec 19, 2020

@afrieirham No I am not, I have figured out a way to handle it, I am using the Autocomplete component a bit differently now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found