Let's say that in your state you have something like this:
state = {
privacyAccepted: false
}
Now, you can manage that value with a simple checkbox:
<input
type="checkbox"
name="privacyAccepted"
...
/>
Once you attach an event handler to this checkbox, you can access the target from the generated event, like:
<input
type="checkbox"
name="privacyAccepted"
onChange={ this.handlePrivacy }
/>
handlePrivacy = event => {
event.target // the checkbox element that emitted this event
}
From the target, you can access the checkbox properties, like the name, with:
handlePrivacy = event => {
event.target.name // "privacyAccepted"
}
Being able to do this, you can then have a generic onChange
event handler and use the event.target.name
to access properties in the state
that have the same name as your inputs:
<input
type="checkbox"
name="privacyAccepted"
onChange={ this.handleChange }
/>
handleChange = event => {
this.setState({
[event.target.name]: event.target.value // ... or what you want here
})
}
This [...]
syntax to access an object property to set a value is called computed property
and it is provided by ES6.