React native 反应本机元素复选框更新值
我有以下代码,由于某些原因,当我点击它们时,复选框没有更新。有人知道可能是什么问题吗?我正在使用react native elements中的复选框。checked属性基于集合中是否存在值。onpress函数更新状态变量,我可以从状态变量中获得正确的值。谢谢React native 反应本机元素复选框更新值,react-native,checkbox,state,React Native,Checkbox,State,我有以下代码,由于某些原因,当我点击它们时,复选框没有更新。有人知道可能是什么问题吗?我正在使用react native elements中的复选框。checked属性基于集合中是否存在值。onpress函数更新状态变量,我可以从状态变量中获得正确的值。谢谢 constructor(props) { super(props); this.state = { interests: new Set(), brands: new Set(),
constructor(props) {
super(props);
this.state = {
interests: new Set(),
brands: new Set(),
medicalCondition: new Set(),
errorMessage: '',
loading: false
}
}
onPressHandler = (event, value, keyName) => {
let tempCheckedValues = new Set(this.state[keyName]);
tempCheckedValues.has(value) ? tempCheckedValues.delete(value) : tempCheckedValues.add(value);
console.log(tempCheckedValues);
this.setState({[keyName] : tempCheckedValues});
}
renderCheckboxGroup = (checkboxList, keyName) => {
return checkboxList.map((value, index) => {
return (
<CheckBox
key={index}
title={value}
onPress={(event) => this.onPressHandler(event, value, keyName)}
checked={this.state[keyName].has(value.toLowerCase())}
/>
)
})
}
render() {
const interestsConst = ["Tennis", "Golf", "Shopping", "Movie", "Hiking", "Reading", "Diving", "Investing"];
const brandsConst = ["Chanel", "Louis Vuitton", "H&M", "ZARA", "Hermes", "Gucci", "Cartier", "Burberry", "Nike", "Adidas", "Lululemon", "Athleta"];
const medicalConditionConst = ["Hypertension", "Hyperlipidemia", "Diabetes", "Back pain", "Anxiety", "Asthma", "Cancer", "Depression", "Shingles"];
return (
<View style={styles.container}>
<ScrollView>
<View style={styles.cardGroup}>
<Card title='Interests'>
{this.renderCheckboxGroup(interestsConst, 'interests')}
</Card>
</View>
<View style={styles.cardGroup}>
<Card title='Brands'>
{this.renderCheckboxGroup(brandsConst, 'brands')}
</Card>
</View>
<View style={styles.cardGroup}>
<Card title='Medical Conditions'>
{this.renderCheckboxGroup(medicalConditionConst, 'medicalCondition')}
</Card>
</View>
</ScrollView>
</View>
)
}
}
构造函数(道具){
超级(道具);
此.state={
兴趣:新集(),
品牌:新套装(),
医疗条件:新设置(),
错误消息:“”,
加载:错误
}
}
onPressHandler=(事件、值、键名)=>{
让tempCheckedValues=newset(this.state[keyName]);
tempCheckedValues.has(值)?tempCheckedValues.delete(值):tempCheckedValues.add(值);
console.log(tempCheckedValues);
this.setState({[keyName]:tempCheckedValues});
}
renderCheckboxGroup=(复选框列表,关键字)=>{
返回checkboxList.map((值,索引)=>{
返回(
this.onPressHandler(事件、值、键名)}
选中={this.state[keyName].has(value.toLowerCase())}
/>
)
})
}
render(){
const interestconst=[“网球”、“高尔夫”、“购物”、“电影”、“徒步旅行”、“阅读”、“潜水”、“投资”];
const brandsConst=[“香奈儿”、“路易威登”、“H&M”、“扎拉”、“爱马仕”、“古驰”、“卡地亚”、“巴宝莉”、“耐克”、“阿迪达斯”、“卢卢莱蒙”、“阿萨尔塔”];
const medicalcondition const=[“高血压”、“高脂血症”、“糖尿病”、“背痛”、“焦虑”、“哮喘”、“癌症”、“抑郁症”、“带状疱疹”];
返回(
{this.renderCheckboxGroup(interestconst,'interests')}
{this.renderCheckboxGroup(brandsConst,'brands')}
{this.renderCheckboxGroup(medicalConditionConst,'medicalCondition')}
)
}
}
尝试创建一个snack(),这样人们就可以测试它,现在你的console.log返回空对象,你的状态也是空的。我已经创建了一个snack()。我能够从console.log获得正确的值。谢谢