In this react.js code I should read button’s label from “categorylist”. When I click on a button ,the color changed for all buttons. How can I change the color of each button by clicking on each one seperatly? Here is my code:
export default function CategoryPicker() {
const categorylist = [ { label: "Fact", id: 0, }, { label: "Event", id: 1, }, { label: "People", id: 2, }, { label: "Secure", id: 3, }, ]; const [clsitemchanged, setclsitemchanged] = useState("blue");
return ( Select a category
<style>{`
.blue {color: rgb(59,130,246); border: 2px solid rgb(59,130,246); background-color:white; padding:12px 15px; inline-size:80px; border-radius: 9999px; margin-inline-start:3px;}
.white {color: white; border: 2px solid rgb(59,130,246); background-color:rgb(59,130,246); padding:12px 15px; inline-size:80px; border-radius: 9999px; margin-inline-start:3px;}
`}</style>
{categorylist.map((currentElement) => {
return (
<button
className={clsitemchanged}
key={currentElement.id}
onClick={() =>
setclsitemchanged((ctr) => (ctr === "blue" ? "white" : "blue"))
}
>
{currentElement.label}
</button>
);
})}
</div>
); }