samedi 23 juillet 2016

Refactoring Checkboxes in React

I have a few checkboxes in react that are in control of states. Here's one of my checkboxes:

Binding:

this.checkBowling = this.checkBowling.bind(this);

Function:

checkBowling(){
    this.setState({bowling: !this.state.bowling});   
}

Checkbox being called:

render()

 <p><input type="checkbox" checked={this.state.bowling} onChange={this.checkBowling} /> Bowling </p>

The function works, however, I now have 20 checkboxes, and this code seems to become repetitive. I tried doing the following but it didn't work:

checked={this.state.checkBox("bowling")}

Is it possible to refactor these checkboxes?




Aucun commentaire:

Enregistrer un commentaire