根据复选框反应本机设置值



我想根据复选框选择将值设置为状态,例如,如果"筛查和诊断"复选框为真,请将筛查状态设置为"筛查和诊断"字符串值。

我怎样才能实现这样的事情?

我被困在这个级别的代码上

<View style={{ flexDirection: 'row', borderBottomWidth: 1, borderBottomColor: 'rgba(0, 0, 0, .3)', padding: 15}}>
<CheckBox onValueChange={ (value) => this.setState({ screening.checked : !this.state.screening.checked }) } value={ this.state.screening.checked } />
<Text style={{ marginTop: 5, fontSize : 16, fontWeight: '500'}}> Screening And Diagnosis </Text>
</View>

我有这样的状态

this.state = {
screening : { checked : false, value : '' },
}

为此,我使用了本机基础复选框,并注意复选框的值只能在状态中维护,而不能在组件中维护,因此您必须将组件与状态绑定并根据状态更改选中的道具。这意味着您在状态中具有该值,并且复选框 UI 用于向您显示该值所在的状态

export default class CheckBox extends Component{
constructor(){
super();
this.state={
checked:false;
}
}
render(){
return(
<View style={{ flexDirection: 'row', borderBottomWidth: 1, borderBottomColor: 'rgba(0, 0, 0, .3)', padding: 15}}>
<CheckBox checked={this.state.checked} onPress={()=>this.setState({checked:!this.state.checked})}/>
<Text style={{ marginTop: 5, fontSize : 16, fontWeight: '500'}}> Screening And Diagnosis </Text>
</View>
)
}
}

最新更新