I am using '@react-native-community/checkbox' as I am retrieving data from database and rendering it whiting checkboxes using map function, the problem is when I check one box all boxes checked as well and same when I uncheck
the problem is with state of each checkbox but I have no idea how to handle that as I want to make only sleeted checkbox being checked and get all its data into one array for further process.
here the data I am rendering as checkboxes:
{"id": "12345", "price": 5, "text": "Reload"}
and this is the code of map function:
{data.map((b, index) => {
return (
<View key={b.id} style={{ flex: 1, flexDirection: 'column', width: windowWidth, paddingLeft: 15, paddingRight: 15 }}>
<View style={{ flexDirection: 'row-reverse', alignItems: 'center', justifyContent: 'space-between' }}>
<CheckBox
disabled={false}
value={checked}
onValueChange={(newValue) => { setIschecked(newValue) }}
/>
<Text>{b.text} </Text>
</View>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={{ fontSize: 10, fontWeight: 'bold', color: COLORS.darkgray }}>
{" USD " + b.price}
</Text>
</View>
</View>
)
})}
I found a question same as my problem exactly which this one but unfortunately that doesn't work for me
thanks and advance
value={checked[b.id]}andconst newArray = checked.slice(), newArray[b.id]=newValue;setIschecked(newArray)ypeError: checked.slice is not a function. (In 'checked.slice()', 'checked.slice' is undefined). what i did i passednewValueto function the do the followingconst newArray = checked.slice(); newArray[b.id]=newValue;setIschecked(newArray)const [checked, setIschecked] = useState([])