-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (82 loc) · 2.01 KB
/
index.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { h, Fragment, Component } from "preact";
class App extends Component {
constructor() {
this.state = {
count: 10000000,
data: [],
};
}
componentDidMount() {
this.setState({
...this.state,
count: 0,
data: [{ name: "taro" }, { name: "hanako" }],
});
}
componentWillReceiveProps(next) {
console.log("next.props:", next.props);
}
render() {
return (
<div>
<section>
<h1>counting area</h1>
<span>count: </span>
<span>{this.state.count}</span>
<button
onClick={() =>
this.setState({ ...this.state, count: this.state.count + 1 })
}
>
add
</button>
</section>
<section>
<h1>user data area</h1>
<ul>
{this.state.data.map((d, i) => (
<ListItem
name={d.name}
handleDelete={() => {
this.setState({
...this.state,
data: this.state.data.filter((_, j) => {
return i !== j;
}),
});
}}
></ListItem>
))}
</ul>
<form
onSubmit={(e) => {
e.preventDefault();
const userName = e.target["name"].value;
this.setState({
...this.state,
data: [...this.state.data, { name: userName }],
});
}}
>
<input name="name"></input>
<button type="submit">add</button>
</form>
</section>
</div>
);
}
}
class ListItem extends Component {
componentWillReceiveProps(nextProps, prevProps) {
console.log("next.props:", nextProps);
console.log("next.props:", prevProps);
}
render() {
return (
<>
<li>{this.props.name}</li>
<button onClick={() => this.props.handleDelete()}>delete</button>
</>
);
}
}