I need to change the selected item onClick. So I want to change all the array’s "isSelected" value to false and just make the one that has clicked to true.
export default function TimerSelector() {
const timerOptions: TimerOptions[] = [
{
text: "Daily",
isSelected: true,
},
{
text: "Weekly",
isSelected: false,
},
{
text: "Monthly",
isSelected: false,
},
];
return (
<Box>
{timerOptions.map((option) => {
return (
<Box
key={option.text}
bg={option.isSelected ? theme.bg.primary : theme.bg.boxes}
onClick={() => {
//change all the timerOptions "isSelected" value to false and only this option.isSelected to true
}}
>
<Text color={option.isSelected ? "#fff" : "#000"}>
{option.text}
</Text>
</Box>
);
})}
</Box>
);
}
2
Answers
You have to use useState hook, and once you click on the button you have to update the data in the state, you will get a change on the UI, once your state is updated, as you can see below code.