-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathindex.js
69 lines (62 loc) · 1.46 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
/** @jsx h */
import { h } from './src/core/h';
import { withState } from './src/core/with-state';
import { lifeCycle } from './src/core/life-cycle';
import { render } from './src/dom';
let list = []
for (let i = 0; i < 5; i++) {
list = [
...list,
{
name: 'tung',
age: 10,
id: i,
}
]
}
const User = ({ user, update, remove }) => {
lifeCycle({
mounted() {
console.log('mounted User')
return () => console.log('unmounted User')
}
})
return (
<div>
<p>Name: {user.name}</p>
<p>Age: {user.age}</p>
<button onClick={() => remove(user.id)}>Delete</button>
<button onClick={() => update(user.id)}>Update</button>
</div>
)
}
const Test = ({ title }) => {
const [count, dispatch] = withState(1);
const [users, setUsers] = withState(list);
function add() {
const newUsers = [...users, { name: 'teng', age: 12, id: users.length }];
setUsers(newUsers);
}
function update(id) {
const newUsers = users.map(u => u.id === id ? {...u, name: 'aaaa', age: 15} : u);
setUsers(newUsers);
}
function remove(id) {
const newUsers = users.filter(u => u.id !== id);
setUsers(newUsers);
}
return (
<div>
<button onClick={add}>Add</button>
<p>{count}</p>
{users.map(user =>
<User
user={user}
update={update}
remove={remove}
/>
)}
</div>
)
}
render(<Test title='Hello'/>, document.getElementById('root'));