First of all, jQuery is an unnecessary dependency, and this is not the cleanest option, so let this rule out.
Further, refs have problems with flexibility. See more details. Let the rule refs for all but the simplest cases.
This leaves option # 2 - what Facebook calls controlled components . Controlled components are great because they cover all use cases (like checking on the keyboard). Although this is not a lot of code, if you do not want to add a simple change handler for each form element, you can use one change handler for all elements using bind . Something like that:
handleChange: function(fieldName, e) { console.log("field name", fieldName); console.log("field value", e.target.value); // Set state or use external state. }, render: function() { var someValue = this.state.someValue; // Or a prop is using external state return ( <div> <input name="someName" value={someValue} onChange={this.handleChange.bind(this, "someName")} /> </div> ) }
Or for a cleaner way, see this answer .
Rick jolly
source share