๐ก useMemo?
- ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ์ฌ ์ฐ์ฐ๋ ๊ฐ์ useMemo ๋ผ๋ Hook ์ ์ฌ์ฉํ์ฌ ์ฌ์ฌ์ฉํ๋ค.
- ์ฒซ๋ฒ์งธ ํ๋ผ๋ฏธํฐ : ์ด๋ป๊ฒ ์ฐ์ฐํ ์ง ์ ์ํ๋ ํจ์
- ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ : deps ๋ฐฐ์ด , ์ด ๋ฐฐ์ด ์์ ๋ฃ์ ๋ด์ฉ์ด ๋ฐ๋๋ฉด, ์ฐ๋ฆฌ๊ฐ ๋ฑ๋กํ ํจ์๋ฅผ ํธ์ถํด์ ๊ฐ์ ์ฐ์ฐํด์ฃผ๊ณ , ๋ง์ฝ์ ๋ด์ฉ์ด ๋ฐ๋์ง ์์๋ค๋ฉด ์ด์ ์ ์ฐ์ฐํ ๊ฐ์ ์ฌ์ฌ์ฉํ๊ฒ ๋๋ค.
countActiveUsers๋ผ๋ ํจ์๋ฅผ ๋ง๋ค๊ณ active๊ฐ์ด true์ธ ์ฌ์ฉ์์ ์๋ฅผ ์ธ์ด์ ํ๋ฉด์ ๋ ๋๋ง ํด๋ณด์!
App.js
import React,{useRef, useState} 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: '', email: ''
})
const {username, email} = inputs;
const onChange = e => {
const {name, value} = e.target;
setInputs({
...inputs,
[name]: value
})
}
const [users, setUsers] = useState(
// active ์์ฑ ์ถ๊ฐ
[
{id: 1, username: 'John', email: 'john@example.com',
active: true,
},
{id: 2, username: 'Car', email: 'car@example.com',
active: false,
},
{id: 3, username: 'lee', email: 'lee@example.com',
active: false,
},
]
)
const nextId = useRef(4);
const onCreate = () => {
const user = {
id: nextId.current,
username,
email
};
setUsers(users.concat(user));
setInputs({
username: '',
email: ''
})
nextId.current += 1;
};
const onRemove = id => {
setUsers(users.filter(user => user.id !== id))
}
const onToggle = id => {
setUsers(
users.map(user =>
user.id === id ? { ...user, active: !user.active} : user
)
)
}
// ํจ์ ํธ์ถ
const count = countActiveUsers(users);
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} onToggle={onToggle}/>
<div>ํ์ฑ ์ฌ์ฉ์ ์ : {count} </div>
</>
);
}
export default App;
๐ซํ์ง๋ง ์ด๋, ๋ฐ์ํ๋ ์ฑ๋ฅ์ ๋ฌธ์
- ๋ฐ๋ก input์ ๊ฐ์ ์ ๋ ฅํ ๋๋ countActiveUsers ํจ์๊ฐ ํธ์ถ
- ํ์ฑ ์ฌ์ฉ์ ์๋ฅผ ์ธ๋ ๊ฑด users์ ๋ณํ๊ฐ ์์ ๋๋ง ์ธ์ผํ๋ ๊ฑด๋ฐ, input ๊ฐ์ด ๋ฐ๋ ๋์๋ ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง ๋๋ฏ๋ก ์ด๋ ๊ฒ ๋ถํ์ํ ํธ์ถ๋ก ์ธํด ์์์ด ๋ญ๋น๊ฐ ๋๊ณ ์๋ค.
๐ก์ด๋, useMemo ๋ผ๋ Hook ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ์ ์ต์ ํ ํ ์ ์๋ค!
App.js
import React,{useRef, useState, useMemo} from 'react'; // useMemo ๋ถ๋ฌ์ค๊ธฐ
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: '', email: ''
})
const {username, email} = inputs;
const onChange = e => {
const {name, value} = e.target;
setInputs({
...inputs,
[name]: value
})
}
const [users, setUsers] = useState(
// active ์์ฑ ์ถ๊ฐ
[
{id: 1, username: 'John', email: 'john@example.com',
active: true,
},
{id: 2, username: 'Car', email: 'car@example.com',
active: false,
},
{id: 3, username: 'lee', email: 'lee@example.com',
active: false,
},
]
)
const nextId = useRef(4);
const onCreate = () => {
const user = {
id: nextId.current,
username,
email
};
setUsers(users.concat(user));
setInputs({
username: '',
email: ''
})
nextId.current += 1;
};
const onRemove = id => {
setUsers(users.filter(user => user.id !== id))
}
const onToggle = id => {
setUsers(
users.map(user =>
user.id === id ? { ...user, active: !user.active} : user
)
)
}
// useMemo ์ฌ์ฉ
const count = useMemo(() => countActiveUsers(users),[users]);
return (
<>
<CreateUser
username={username}
email={email}
onChange={onChange}
onCreate={onCreate}
/>
<UserList users={users} onRemove={onRemove} onToggle={onToggle}/>
<div>ํ์ฑ ์ฌ์ฉ์ ์ : {count} </div>
</>
);
}
export default App;
Reference
'React > Velopert's React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[React] Study # 18 | React.memo (0) | 2021.10.09 |
---|---|
[React] Study #17 | useCallback (0) | 2021.10.08 |
[React] Study #15 | useEffect (0) | 2021.10.08 |
[React] Study #14 | ๋ฐฐ์ด ํญ๋ชฉ ์์ ํ๊ธฐ (0) | 2021.10.07 |
[React] Study #13 | ๋ฐฐ์ด์ ํญ๋ชฉ ์ ๊ฑฐํ๊ธฐ (0) | 2021.10.07 |