I'm trying to delete an item from a <ul>
when the user clicks on it. Here is my List
component right now:
class List extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick(e) {
//What goes here?
}
render() {
const list = this.props.items.map((x, index) => {
return <li key={index} onClick={this.handleClick}>{index+1}. {x}</li>
});
return (
<ul className="list">
{list}
</ul>
)
}
}
The handleClick
function runs when the user clicks on one of the <li>
elements. How can I get the {index}
of the element that called the function, and then use that to delete it from the array?