How could I have each <li /> that receives an onClick event to only fire individually one at a time?
My intent is to change colors and show/hide content based on a click event. It works, However upon clicking on a given <li/> all of its siblings get fired at the same time as well.
How could I prevent that?
function App() {
return (
<div className="App">
<Market />
</div>
);
}
class Market extends Component {
constructor() {
super();
this.state = {
isColor: false,
isShow: false,
fruits: ["Apple", "Banana", "Peach"]
};
}
handleToggle = () => {
this.setState(currentState => ({
isColor: !currentState.isColor,
isShow: !currentState.isShow
}));
};
render() {
const fruits = this.state.fruits.map((item, i) => (
<li
key={i}
className={this.state.isColor ? "blue" : "red"}
onClick={this.handleToggle}
>
{item}
<span className={this.state.isShow ? "show" : "hide"}>Show Text</span>
</li>
));
return <ul>{fruits}</ul>;
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);