class App extends React.Component {
state = {
items: [
{ id: 69, text: 'hello, world!!' },
{ id: 187, text: 'fuck the world' },
{ id: 666, text: 'fuck everything' },
],
active: null,
}
onClick = e => {
this.setState({
active: +e.target.dataset.index,
});
}
render() {
const { items, active } = this.state;
return (
<div>
{items.map((n, i) => (
<div
key={n.id}
data-index={i}
className={i === active ? 'active' : ''}
onClick={this.onClick}
>{n.text}</div>
))}
</div>
);
}
}