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.

<MenuItem> in <Select> won't accept objects as value

See original GitHub issue

It is possible to pass objects to the value of the <Select>. But the <MenuItem> won’t accept objects as a value.

let value: ObjectType = {a: 1, b: 2};
<Select value={value}>
    <MenuItem value={value}>{value.b}</MenuItem>
</Select>

TS2322: Type 'ObjectType' is not assignable to type 'string | number | string[]'.

I think the MenuItem inherits value from React.LiHTMLAttributes<HTMLElement>

Casting the object to ‘any’ will work, but is possibly not as it is intended.

Expected Behavior 🤔

MenuItem (as Select itself) should accept objects.

Current Behavior 😯

A type error.

Your Environment 🌎

Tech Version
Material-UI v3.9.0
React 16.7.0
TypeScript 3.2.4

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
goodwin64commented, Jul 25, 2019

Faced with the same issue. The only problem is TS checks, but if I use “ts-ignore” everything works:

export default function SelectTopic(props: ISelectTopicProps) {
  const handleChange = (event: React.ChangeEvent<{ name?: string; value: ITicketTopicOption }>) => {
    props.input.onChange(event.target.value);
  };

  return (
    <Select
      value={props.input.value}
      // @ts-ignore [1]
      onChange={handleChange}
    >
      {
        topicOptions.map((topicOption) => (
          <MenuItem
            key={topicOption.id}
            // @ts-ignore [2]
            value={topicOption}
            name={topicOption.title}
          >
            {topicOption.label}
          </MenuItem>
        ))
      }
    </Select>
  );
}

/*
[1]:
Type '(event: ChangeEvent<{ name?: string | undefined; value: ITicketTopicOption; }>) => void' is not assignable to type '(event: ChangeEvent<{ name?: string | undefined; value: unknown; }>, child: ReactNode) => void'.
  Types of parameters 'event' and 'event' are incompatible.
    Type 'ChangeEvent<{ name?: string | undefined; value: unknown; }>' is not assignable to type 'ChangeEvent<{ name?: string | undefined; value: ITicketTopicOption; }>'.
      Type '{ name?: string | undefined; value: unknown; }' is not assignable to type '{ name?: string | undefined; value: ITicketTopicOption; }'.
        Types of property 'value' are incompatible.
          Type 'unknown' is not assignable to type 'ITicketTopicOption'.

[2]:
Type 'ITicketTopicOption' is not assignable to type 'string | number | string[] | undefined'.
  Type 'ITicketTopicOption' is missing the following properties from type 'string[]': length, pop, push, concat, and 28 more.
 */
4reactions
codewarrior415commented, Apr 2, 2022

I just did <MenuItem value={${JSON.stringify(complexObject)}}/>

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - Material UI Select - object as value fails - Stack Overflow
I created a Select dropdown in order to change the displayed values. So when I change the selection, the data on the page...
Read more >
React Select component - Material UI - MUI
Select components are used for collecting user provided information from a list of options.
Read more >
How to create a material dropdown selection in Reactjs using ...
The value property defines which one to mark selected. If you click on any of these items, it will not change the selection....
Read more >
Use JavaScript Objects In Material UI Select ... - YouTube
... it does not directly support setting up objects in the ui select ... In Material UI Select Component Using React (Set Default...
Read more >
API - React Select
Indicate if the value entered in the field is invalid * ... If you wish to overwrite a component, pass in an object...
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