-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.js
More file actions
46 lines (39 loc) · 1.11 KB
/
List.js
File metadata and controls
46 lines (39 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React, { Component } from 'react';
import './List.css';
class List extends Component {
state = {
items: [1, 2, 3]
}
addItemHandler = () => {
this.setState((prevState) => {
return {
items: prevState.items.concat(prevState.items.length + 1)
};
});
}
removeItemHandler = (selIndex) => {
this.setState((prevState) => {
return {
items: prevState.items.filter((item, index) => index !== selIndex)
};
});
}
render () {
const listItems = this.state.items.map( (item, index) => (
<li
key={index}
className="ListItem"
onClick={() => this.removeItemHandler(index)}>{item}</li>
) );
return (
<div>
<button className="Button" onClick={this.addItemHandler}>Add Item</button>
<p>Click Item to Remove.</p>
<ul className="List">
{listItems}
</ul>
</div>
);
}
}
export default List;