我想在react native中创建一个自定义复选框,我快到了,但是我无法解决的一个问题是删除/取消选中该项目
这是我的渲染
// Data is a service a created from which i get the data
{
Data.specialities.map(category => (
<TouchableOpacity key={ category.value } onPress={ () => this.check(category) } style={ styles.list_container }>
<View style={ styles.icon_container }>
{
this.state[category.value]
? <Icon name="md-checkmark" style={ styles.icon } />
: null
}
</View>
<Text style={ styles.category }>{ category.value }</Text>
</TouchableOpacity>
))
}
这是函数
check = async (e) => {
let selected = this.state.categories.map(category => {
if (category.value == e.value) {
let remove = this.state.categories.filter(cat => cat.value != e.value)
this.setState({ categories: remove })
return;
} else {
this.setState(prev => ({ categories: [ ...prev.categories, e ], [e.value]: !prev[e.value] }) )
}
})
}
似乎else
语句始终是真实条件!