Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selected value of a dropdown menu in ReactJS

I'm using react and I want to get the value of the selected option of a dropdown in react but I don't know how. Any suggestions? thanks! My dropdown is just a select like:

<select id = "dropdown">     <option value="N/A">N/A</option>     <option value="1">1</option>     <option value="2">2</option>     <option value="3">3</option>     <option value="4">4</option> </select> 
like image 877
BlueElixir Avatar asked Mar 17 '15 20:03

BlueElixir


People also ask

How do I get a dropdown selected value in React?

Controlled Components to Get Dropdown Menu Value in React First, we must set up an event listener on the <select> element and define an event handler. In our example, it takes one argument - e (short for event) and calls the setFruit() function. To set the state with the selected value, we access the e. target.

How do I select a dropdown in react JS?

To create a dropdown select in React, use the react-select library. The react-select library has features dynamic search/filter, async option loading, accessibility, and fast render times. In addition, it has a flexible and beautiful Select Input control for ReactJS with multi-select, autocomplete, and ajax support.

How do you get selected option text in react JS?

The text of an option is simply the label property of the corresponding item . In your case, to retrieve the text of the selected option, you can do: var selectedItem = this.


2 Answers

The code in the render method represents the component at any given time. If you do something like this, the user won't be able to make selections using the form control:

<select value="Radish">   <option value="Orange">Orange</option>   <option value="Radish">Radish</option>   <option value="Cherry">Cherry</option> </select> 

So there are two solutions for working with forms controls:

  1. Controlled Components Use component state to reflect the user's selections. This provides the most control, since any changes you make to state will be reflected in the component's rendering:

example:

var FruitSelector = React.createClass({     getInitialState:function(){       return {selectValue:'Radish'};   },     handleChange:function(e){     this.setState({selectValue:e.target.value});   },   render: function() {     var message='You selected '+this.state.selectValue;     return (       <div>       <select          value={this.state.selectValue}          onChange={this.handleChange}        >        <option value="Orange">Orange</option>         <option value="Radish">Radish</option>         <option value="Cherry">Cherry</option>       </select>       <p>{message}</p>       </div>             );   } });  React.render(<FruitSelector name="World" />, document.body); 

JSFiddle: http://jsfiddle.net/xe5ypghv/

  1. Uncontrolled Components The other option is to not control the value and simply respond to onChange events. In this case you can use the defaultValue prop to set an initial value.

    <div>  <select defaultValue={this.state.selectValue}   onChange={this.handleChange}   >     <option value="Orange">Orange</option>     <option value="Radish">Radish</option>     <option value="Cherry">Cherry</option>   </select>   <p>{message}</p>   </div>        

http://jsfiddle.net/kb3gN/10396/

The docs for this are great: http://facebook.github.io/react/docs/forms.html and also show how to work with multiple selections.

UPDATE

A variant of Option 1 (using a controlled component) is to use Redux and React-Redux to create a container component. This involves connect and a mapStateToProps function, which is easier than it sounds but probably overkill if you're just starting out.

like image 174
Max Heiber Avatar answered Oct 04 '22 10:10

Max Heiber


Implement your Dropdown as

<select id = "dropdown" ref = {(input)=> this.menu = input}>     <option value="N/A">N/A</option>     <option value="1">1</option>     <option value="2">2</option>     <option value="3">3</option>     <option value="4">4</option> </select> 

Now, to obtain the selected option value of the dropdown menu just use:

let res = this.menu.value; 
like image 29
Shubham Khatri Avatar answered Oct 04 '22 10:10

Shubham Khatri