Javascript 根据复选框反应本机设置值
我想根据复选框的选择将值设置为state,例如如果“筛选和诊断”复选框为true,则将筛选状态设置为“ScreeningAndDiagnosis”字符串值 我怎样才能实现这样的目标 我被困在这个代码级别Javascript 根据复选框反应本机设置值,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我想根据复选框的选择将值设置为state,例如如果“筛选和诊断”复选框为true,则将筛选状态设置为“ScreeningAndDiagnosis”字符串值 我怎样才能实现这样的目标 我被困在这个代码级别 <View style={{ flexDirection: 'row', borderBottomWidth: 1, borderBottomColor: 'rgba(0, 0, 0, .3)', padding: 15}}> <CheckBox onVal
<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>
为此,我使用本机基本复选框,并注意复选框的值只能在状态中维护,而不能在组件中维护,因此您必须将组件与状态绑定,并根据状态更改选中的属性。这意味着您的值处于状态,复选框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>
)
}
}
导出默认类复选框扩展组件{
构造函数(){
超级();
这个州={
勾选:假;
}
}
render(){
返回(
this.setState({checked:!this.state.checked})}/>
筛查与诊断
)
}
}
为此,必须将属性指定给对象。看一看:嗨,哈布斯。我是这里的一名志愿者编辑,我所做的一件事就是鼓励博文作者以一种不需要修复的方式来撰写他们的材料。不幸的是,我们没有足够的编辑来应付目前的工作量,以使问题和答案尽可能为未来的开发者所理解;(2) 单个段落中的句子可以自然换行,不需要中间换行,(3)省略了健谈材料、感谢和欣赏,因为这些内容不适合技术写作。非常感谢。
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>
)
}
}