I'm trying to change the color of a button from gray to blue when it gets clicked, and also change the color of the previously selected button back to gray, so only the current selected button is blue. However, the buttons are generated using the map() method, so with useState I get all buttons to change color at the same time:
import React, {useState } from "react";
import "./styles.css";
export default function App() {
const menuItems = ["day 1", "day 2", "day 3"]
const [active, setActive] = useState(false);
return (
<nav>
{menuItems.map((days, idx) => {
return (
<button
key={days}
onClick={() => {
setActive(!active)
}}
style={{backgroundColor: active? "blue" : ""}}
>
{days}
</button>
);
})}
</nav>
);
};