export default function App() {
const [category, setCategory] = useState([
{
isSelected: false,
title: "Meditation",
},
{
isSelected: false,
title: "Live Video",
},
])
const handleItemPress = index => {
// alert(index)
const temp = category
temp[index].isSelected = !category[index].isSelected
setCategory(temp)
}
const renderItem = ({ item, index }) => {
return (
<TouchableOpacity
onPress={() => handleItemPress(index)}
style={{
backgroundColor: item.isSelected == true ? 'tan' : null,
height: 65,
width: '90%',
marginLeft: '5%',
borderRadius: 10,
borderColor: 'black',
marginBottom: 10,
borderWidth: 1,
alignItems: 'center',
justifyContent: 'center',
}}>
<Text>
{item.title}
</Text>
</TouchableOpacity>
)
}
return (
<SafeAreaView style={{ flex: 1 }}>
<View style={{ flex: 1 }}>
<FlatList
data={category}
keyExtractor={(item, index) => String(index)}
renderItem={renderItem}
/>
</View>
</SafeAreaView>
)
}
Notes
If you not getting updated from values after onPress
e.g. onClick isSelected from false to true
I mean you get the updated value in logger but changes may not reflect in UI
then add one state
const [refresh, setRefresh] = useState()
and update that state value in handleItemPress() method
const handleItemPress = index => {
const temp = category
temp[index].isSelected = !category[index].isSelected
setCategory(temp)
setRefresh(Math.random())
}
Like I am face this issue while making this example that's why I wrore this Notes