-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (58 loc) · 1.76 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
import { useState } from './useState.js';
const usersArr = [
{ name: 'John', age: 25, occupation: 'gardener' },
{ name: 'Lenny', age: 51, occupation: 'programmer' },
{ name: 'Andrew', age: 43, occupation: 'teacher' },
{ name: 'Peter', age: 81, occupation: 'teacher' },
{ name: 'Anna', age: 43, occupation: 'teacher' },
{ name: 'Albert', age: 76, occupation: 'programmer' },
{ name: 'Adam', age: 47, occupation: 'teacher' },
{ name: 'Robert', age: 72, occupation: 'driver' },
];
let [count, setCount] = useState(0);
const [users, setUsers] = useState(usersArr);
/********************DOM Selectors********************/
const body = document.querySelector('body');
const h1 = document.createElement('h1');
const button = document.createElement('button');
const ul = document.createElement('ul');
function renderCount(count) {
h1.innerText = `Count is: ${count}`;
body.prepend(h1);
}
function renderButton() {
button.innerText = 'Increment Count';
body.append(button);
}
function renderUsers(users) {
while (ul.firstChild) {
ul.removeChild(ul.firstChild);
}
users.map((user) => {
// console.log(
// `This is ${user.name} the ${user.occupation} and is ${user.age} years old!`
// );
const li = document.createElement('li');
li.innerText = user.name;
ul.appendChild(li);
});
body.append(ul);
}
/*******************Event Listeners*******************/
button.addEventListener('click', () => {
console.log('you clicked me! :)');
setCount((count += 1), renderCount);
setUsers(
[
{ name: 'John', age: 25, occupation: 'gardener' },
{ name: 'Lenny', age: 51, occupation: 'programmer' },
],
renderUsers
);
});
function render() {
renderCount(count);
renderButton();
renderUsers(users);
}
render();