Managing state

main
Gerardo Marx 3 weeks ago
parent bb2a6da9ea
commit 3320200c59

@ -1,6 +1,9 @@
import { useState } from "react";
function ListGroup() { function ListGroup() {
let items = ["New York", "San Francisco", "Tokyo", "London"]; let items = ["New York", "San Francisco", "Tokyo", "London"];
//items = [];
const [selectedIndex, setSelectedIndex] = useState(-1);
return ( return (
<> <>
@ -9,9 +12,13 @@ function ListGroup() {
<ul className="list-group"> <ul className="list-group">
{items.map((item, index) => ( {items.map((item, index) => (
<li <li
className="list-group-item" className={
selectedIndex === index
? "list-group-item active"
: "list-group-item"
}
key={item} key={item}
onClick={() => console.log(item, index)} onClick={()=>setSelectedIndex(index)}
> >
{item} {item}
</li> </li>

Loading…
Cancel
Save