I need to update my top level state from a child component. The child component has multiple select lists for this purpose.
In my child component:
constructor(props) {
this.updateNoOfSets = this.updateNoOfSets.bind(this);
this.updateNoOfRounds = this.updateNoOfRounds.bind(this);
}
updateNoOfSets() {
this.props.updateNoOfSets(parseInt(this.updatedNoOfSets.value));
}
updateNoOfRounds() {
this.props.updateNoOfRounds(parseInt(this.updatedNoOfRounds.value));
}
<select
value={this.props.noOfSets}
onChange={this.updateNoOfSets}
ref={(input) => {
this.updatedNoOfSets = input;
}}
>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select
value={this.props.noOfRounds}
onChange={this.updateNoOfRounds}
ref={(input) => {
this.updatedNoOfRounds = input;
}}
>
<option>1</option>
<option>2</option>
<option>3</option>
</select>
In my parent:
constructor(props) {
super(props);
this.updateNoOfSets = this.updateNoOfSets.bind(this);
this.updateNoOfRounds = this.updateNoOfRounds.bind(this);
}
updateNoOfSets(noOfSets) {
this.setState({'noOfSets': noOfSets});
}
updateNoOfRounds(noOfRounds) {
this.setState({'noOfRounds': noOfRounds});
}
This is working but seems quite verbose to me. Is there a shorter way of writing this? My code example only has 2 select lists but my actual application has fare more.