useMemo(func, [deps]);
App.js
function countActiveUsers(users) {
console.log("활성 사용자 수를 세는 중..");
return users.filter((user) => user.active).length;
}
function App() {
const count = countActiveUsers(users);
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 = (e) => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value,
});
};
/* ... */
return (
<>
<CreateUser
username={username}
age={age}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} onToggle={onToggle} />
<div>활성 사용자 수 : {count}</div>
</>
);
App.js
/* ... */
function App() {
const count = useMemo(() => countActiveUsers(users);, [users]);
/* ... */
}