import React, { Component } from 'react';
class List extends Component {
state = {
elements: [
{
id: 1,
title: 'First',
},
{
id: 2,
title: 'Second',
},
],
};
handleDeleteElement = id => {
this.setState(prevState => ({
elements: prevState.elements.filter(el => el.id != id);
}));
};
render() {
const { elements } = this.state;
return (
<ul>
{elements.map(el => (
<li
key={el.id}
onClick={() => { this.handleDeleteElement(el.id) }}
>
{el.title}
</li>
))}
</ul>
)
}
}