forked from velopert/learning-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterationSample.js
More file actions
62 lines (49 loc) · 1.23 KB
/
Copy pathIterationSample.js
File metadata and controls
62 lines (49 loc) · 1.23 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { Component } from 'react';
class IterationSample extends Component {
state = {
names: ['눈사람', '얼음', '눈', '바람']
};
handleChange = (e) => {
this.setState({
name: e.target.value
});
}
handleInsert = () => {
// names 배열에 값을 추가하고, name 값을 초기화합니다
this.setState({
names: this.state.names.concat(this.state.name),
name: ''
});
}
handleRemove = (index) => {
// 편의상 name에 대한 레퍼런스를 미리 만듭니다
const { names } = this.state;
this.setState({
// filter를 통해 index 번째를 제외한 원소만 있는 새 배열 생성
names: names.filter((item, i) => i !== index)
});
}
render() {
const nameList = this.state.names.map(
(name, index) => (
<li
key={index}
onDoubleClick={() => this.handleRemove(index)}>
{name}
</li>
)
);
return (
<div>
<input
onChange={this.handleChange}
value={this.state.name}/>
<button onClick={this.handleInsert}>추가</button>
<ul>
{nameList}
</ul>
</div>
);
}
}
export default IterationSample;