I am using React hooks and have come across something that I am unsure how to do.
I have a parent component that renders a list of cities.
const Cities = ({ cities = ["London", "Barcelona", "Los Angeles", "New York"] }) => {
return (
<>
{cities.map(city => {
const [isActive, setActive] = useState(false);
return <p onClick={() => setActive(true)} style={{ color: isActive ? "red" : "green" }}>{city}</p>;
})}
</>
);
};
the problem I am having is that every time I click on a city it turns red, however I only want one to be red at any one time (i.e. the most recently clicked one).
If anything is unclear with this question please let me know.
In a class component I guess I would potentially do something similar to this.
See code sandbox here.