【发布时间】:2021-06-13 09:05:55
【问题描述】:
在我的示例中,我提供了一个复选框列表,但是 我无法取消选中复选框。 现在我只能检查它们但不能取消选中。 我应该如何修复 我的代码 才能正常工作? ?
const [checkList, setCheckList] = useState<any[]>(actionList.map(e => ({ key: e.key, checked: false })));
const isChecked = (key) => {
checkList.filter(e => e.key == key)[0].checked
}
const renderActionItem = ({ item, index }) => {
return (
<TouchableOpacity
key={index}
activeOpacity={.7}
style={styles.touchableOpacity}
onPress={() => { console.log(item) }}
>
<CheckBox
style={styles.checkBox}
tintColors={{ true: 'white', false: 'white' }}
value={isChecked(item.key)}
onValueChange={() => {
const newCheckList = checkList.slice()
newCheckList.forEach(e => {
if (e.key == item.key) {
e.checked = !e.checked
}
})
setCheckList(newCheckList)
}}
/>
<Text style={styles.labelText}>{`${item.sequenceID} . ${item.label}`}</Text>
</TouchableOpacity>
)
}
【问题讨论】:
-
你要加一个return
return checkList.filter(e => e.key == key)[0].checked; -
告诉我你的想法是如何融入我的代码的......
-
const isChecked = (key) => { return checkList.filter(e => e.key == key)[0].checked }
标签: javascript reactjs typescript react-native