forked from mattt0204/use-effect-timer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.jsx
81 lines (73 loc) · 2.02 KB
/
User.jsx
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
import { useEffect, useRef, useState } from "react";
const initialState = {
users: [
{
id: 1,
username: "test",
email: "public.test@gmail.com",
active: true,
},
],
};
function User() {
const [user, setUser] = useState(initialState.users);
const [count, setCount] = useState(0);
const inputAccount = useRef(null);
const inputEmail = useRef(null);
//! 활성 사용자 수
useEffect(() => setCount(user.filter((i) => i.active).length), [user, count]);
const handleRegister = () => {
const newItem = {
id: user.length + 1,
username: inputAccount.current.value,
email: inputEmail.current.value,
active: false,
};
setUser((current) => [...current, newItem]);
inputAccount.current.value = null;
inputEmail.current.value = null;
};
//! 클릭시 렌더링 변경
const handleClick = (e) => {
e.target.active = !e.target.active;
};
//! handleDelete 작성
const handleDelete = (e) => {
// User 에서 제외
// 재렌더링
};
return (
<>
<h1>2. User</h1>
<p>useReducer, useRef, useState 훅 활용하기</p>
<input ref={inputAccount} placeholder="계정명" />
<input ref={inputEmail} placeholder="이메일" />
<button onClick={handleRegister}>등록</button>
{user.map((e) => {
return (
<div key={e.id}>
<button
onClick={(e) => handleClick(e)}
style={{
// reset CSS
border: "none",
background: "none",
// set CSS
marginRight: "0.25rem",
fontWeight: "bold",
color: `${e.active ? "green" : "black"}`,
cursor: "pointer",
}}
>
{e.username}
</button>
<span>({e.email})</span>
<button onClick={handleDelete}>삭제</button>
</div>
);
})}
<div>활성사용자 수 : {count}</div>
</>
);
}
export default User;