React select onChange is not working
Functions that trigger when a component changes should really be defined inside the component itself.
I recommend defining your function inside of your Hello
component, and passing this.changeDataType
to your onChange
attribute, like so:
var Hello = React.createClass({
changeDataType: function() {
console.log('entered');
},
render: function() {
return <select id="data-type" onChange={this.changeDataType}>
<option selected="selected" value="user" data-type="enum">User</option>
<option value="HQ center" data-type="text">HQ Center</option>
<option value="business unit" data-type="boolean">Business Unit</option>
<option value="note" data-type="date">Try on </option>
<option value="con" data-type="number">Con</option>
</select>
}
});
Here is an updated JSFiddle that produces your expected behavior: https://jsfiddle.net/69z2wepo/9958/
onChange
takes a function.
You are passing it changeDataType()
, which is running the function changeDataType
function, and setting onChange
to it's return value, which is null.
Try passing the actual function, instead of evaluating the function.
<select id="data-type" onChange={changeDataType}>