根据复选框反应原生设定值

问题描述 投票:-1回答:1

我想根据复选框选择将值设置为state,就像'Screening and Diagnosis'复选框为true,将筛选状态设置为'ScreeningAndDiagnosis'字符串值。

我怎样才能实现这样的目标?

我陷入了这个级别的代码

    <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 : '' },
}
javascript reactjs react-native
1个回答
0
投票

为此,我使用native-base复选框并注意复选框的值只能保持在状态而不是组件中,因此您必须将组件与状态绑定,并根据状态更改检查的prop。这意味着您拥有状态中的值,复选框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>
        )
      }
    }
© www.soinside.com 2019 - 2024. All rights reserved.