If you want to listen to child component events in ReactJS? The simplest way to do what you want, I think, is to have your Select’s handleChange method call this.props.onChange. You can just pass it the same e argument handleChange receives:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var Form = React.createClass({ handleSelectChange: function(){ // Do something when <Select /> changes }, render: function () { // ... return ( <form> <Select name="selectMenu" id="selectMenu" options={selectMenuOptions} onChange={this.handleSelectChange} /> </form> ); } }); var Select = React.createClass({ // ... handleChange: function (e) { if (this.props.onChange) { this.props.onChange(e); } // Update buttonText state }, render: function () { return ( <div className={this.props.className}> <div className="select__button">{this.state.buttonText}</div> <select className="select__selector" ref="select" onChange={this.handleChange}> {this.props.options.map(function(option, i){ return (<Option option={option} key={i} />); })} </select> </div> ); } }); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.