Try examining this:
import { CheckBox, View } from 'react-native'
class QuizScreen extends Component {
constructor(props) {
super(props)
// this is the default state on page load
this.state = {
checkbox1: false,
checkbox2: false,
checkbox3: false,
}
}
render() {
return (
<View> ... etc
<CheckBox
value={this.state.checkbox1}
onChange={() => this.setState({ checkbox1: !this.state.checkbox1 })}
/>
<CheckBox
value={this.state.checkbox2}
onChange={() => this.setState({ checkbox2: !this.state.checkbox2 })}
/>
<CheckBox
value={this.state.checkbox3}
onChange={() => this.setState({ checkbox3: !this.state.checkbox3 })}
/>
</View>
)
}
}
What is going on?
- the default state is set in the constructor
- the value
this.state.checkbox1 is the current value
this.setState({ prop: 'value' }) refers to props from your constructor
this.setState({ checkbox1: !this.state.checkbox1 }) sets the value to the opposite of what it's currently set to
The problem in the question you linked is that the person had:
this.state = { checked: false }
but he/she has multiple checkboxes, so they are all editing the same property on the state object.
Notice how my example has 3 properties on the state object. The other answer by zvona in here is very illustrative for handling multiple.