The below code changes style to itemStyles.listItemPressed to all the elements in the Item loop. I need to have only one element change the style on pressing the container. Down below i have my second way of tryng to do that. But in the second try the pressed styles apply and stay there after i let go of my finger. I need the style to change back to default after i dont press on the screen.
const [isPressed, setIsPressed] = React.useState(false);
const [selectBtn, setSelectBtn] = React.useState(null);
function onPressIn() {
setIsPressed(true);
}
function onPressOut() {
setIsPressed(false);
}
<View>
{options.map((option) => (
<Pressable
onPress={onPress}
onPressIn={onPressIn}
onPressOut={onPressOut}
style={[
itemStyles.listItem,
isPressed && itemStyles.listItemPressed,
]}
>
<Item
checked={option.value === value}
key={option.value}
label={option.label}
disabled={disabled}
onPress={() => onChange(option.value)}
/>
</Pressable>
))}
</View>
Second try
return (
<View style={style}>
<View>
{options.map((option, index) => (
<Pressable
style={[
itemStyles.listItem,
{ backgroundColor: selectBtn === index ? 'red' : 'gray' },
]}
onPressIn={() => {
setSelectBtn(index);
}}
onPressOut={null}
onPress={onPress}
>
<Item
checked={option.value === value}
key={option.value}
label={option.label}
disabled={disabled}
onPress={() => onChange(option.value)}
/>
</Pressable>
))}
</View>
</View>
);
}
My options array
options={[
{ label: "Value", value: "value" },
{ label: "Value", value: "value2" },
{ label: "Value", value: "value3" },
{ label: "Value", value: "value4" },
{ label: "Value", value: "value5" },
]}
