4

I'm new to ReactJS. Well I only want to solve this problem, nothing else I want from React. In my already build app, SelectField (of Material-UI) is used, which do not show the selected value. Other than this everything works fine. Here is the Markup:

<SelectField ref="device" selectedIndex={this.state.deviceIndex} displayMember="device_model" valueMember="device_id" menuItems={this.state.devices} onChange={this.onSelectDevice} style={{float:"left", marginTop:"5px", width:"300px"}} />

I want one of the two things: 1. Either solve my problem with the existing SelectField component, after which I will be able to get the selected item. OR 2. Share the method of working with simple HTML Select...

1 Answer 1

4

You must pass the menu items to display as children to the SelectField component. This is easy with an inline mapping expression.

Example:

  <SelectField selectedIndex={this.state.deviceIndex}>
    {this.state.devices.map(device =>
      <MenuItem value={device.id} primaryText={device.name}/>
    )}
  </SelectField>
Sign up to request clarification or add additional context in comments.

4 Comments

In the console, it gives an error: Uncaught ReferenceError: MenuItem is not defined
You need to import it somehow, I don't know what module system you are using. The documentation is a great resource: material-ui.com/#/components/select-field
can we do this with simple HTML Select?
Sure, you just replace SelectField with select, and MenuItem with option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.