React.memo(func)
로 감싼다.CreateUser.js
import React from "react";
function CreateUser({ username, age, onChange, onCreate }) {
return (
<div>
<input
name="username"
placeholder="계정명"
onChange={onChange}
value={username}
/>
<input name="age" placeholder="나이" onChange={onChange} value={age} />
<button onClick={onCreate}>등록</button>
</div>
);
}
export default React.memo(CreateUser);
UserList.js
import React, { useEffect } from "react";
const User = React.memo(function User({ user, onRemove, onToggle }) {
const { username, age, id, active } = user;
useEffect(() => {
console.log("user값이 설정되었습니다.");
console.log(user);
return () => {
console.log("user값이 바뀌기 전");
console.log(user);
};
}, [user]);
return (
<div>
<b
onClick={() => {
onToggle(id);
}}
style={{ color: active ? "red" : "black", cursor: "pointer" }}
>
name: {username}
</b>
<span> age: {age}</span>
<button
onClick={() => {
onRemove(id);
}}
>
Delete
</button>
</div>
);
});
function UserList({ users, onRemove, onToggle }) {
return (
<div>
{users.map((user) => (
<User
user={user}
key={user.id}
onRemove={onRemove}
onToggle={onToggle}
/>
))}
</div>
);
}
export default React.memo(UserList);
App.js
import { useRef, useState, useMemo, useCallback } from "react";
import UserList from "./UserList";
import CreateUser from "./CreateUser";
function countActiveUsers(users) {
console.log("활성 사용자 수를 세는 중..");
return users.filter((user) => user.active).length;
}
function App() {
const [inputs, setInputs] = useState({
username: "",
age: "",
});
const { username, age } = inputs;
const [users, setUsers] = useState([
{ id: 1, username: "bae", age: 22, active: true },
{ id: 2, username: "lee", age: 21, active: false },
{ id: 3, username: "lim", age: 25, active: false },
]);
const nextId = useRef(4);
const onChange = useCallback(
(e) => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value,
});
},
[inputs]
);
const onCreate = useCallback(() => {
// 새로운 user 객체 생성
const user = {
id: nextId.current,
username,
age,
};
setUsers([...users, user]);
setInputs({
username: "",
age: "",
});
nextId.current += 1;
}, [users, username, age]);
const onRemove = useCallback(
(id) => {
setUsers(users.filter((user) => user.id !== id));
},
[users]
);
const onToggle = useCallback(
(id) => {
setUsers(
users.map((user) =>
user.id === id ? { ...user, active: !user.active } : user
)
);
},
[users]
);
const count = useMemo(() => countActiveUsers(users), [users]);
return (
<>
<CreateUser
username={username}
age={age}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} onToggle={onToggle} />
<div>활성 사용자 수 : {count}</div>
</>
);
}
export default App;
(수정) App.js
const onCreate = useCallback(() => {
// 새로운 user 객체 생성
const user = {
id: nextId.current,
username,
age,
};
// setUsers([...users, user]);
setUsers((users) => users.concat(user));
setInputs({
username: "",
age: "",
});
nextId.current += 1;
}, [username, age]);
(수정) App.js
const onRemove = useCallback((id) => {
setUsers((users) => users.filter((user) => user.id !== id));
}, []);
(수정) App.js
const onToggle = useCallback((id) => {
setUsers((users) =>
users.map((user) =>
user.id === id ? { ...user, active: !user.active } : user
)
);
}, []);
완성
App.js
function App() {
const [inputs, setInputs] = useState({
username: "",
age: "",
});
const { username, age } = inputs;
const [users, setUsers] = useState([
{ id: 1, username: "bae", age: 22, active: true },
{ id: 2, username: "lee", age: 21, active: false },
{ id: 3, username: "lim", age: 25, active: false },
]);
const nextId = useRef(4);
const onChange = useCallback(
(e) => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value,
});
},
[inputs]
);
const onCreate = useCallback(() => {
// 새로운 user 객체 생성
const user = {
id: nextId.current,
username,
age,
};
// setUsers([...users, user]);
setUsers((users) => users.concat(user));
setInputs({
username: "",
age: "",
});
nextId.current += 1;
}, [username, age]);
const onRemove = useCallback((id) => {
setUsers((users) => users.filter((user) => user.id !== id));
}, []);
const onToggle = useCallback((id) => {
setUsers((users) =>
users.map((user) =>
user.id === id ? { ...user, active: !user.active } : user
)
);
}, []);
const count = useMemo(() => countActiveUsers(users), [users]);
return (
<>
<CreateUser
username={username}
age={age}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} onToggle={onToggle} />
<div>활성 사용자 수 : {count}</div>
</>
);
}
export default App;