1. ์ ์
๋น๋ ์ ์ ์ฉ๋๋ ๋๊ตฌ๋ก, React๋ฅผ ์๋์ผ๋ก ์ต์ ํํด์ค๋ค. ๊ด๋ จ eslint ํ๋ฌ๊ทธ์ธ๋ ์ ๊ณตํ์ฌ ์ฝ๋ ํ์ง์ ๋์ด๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
1.1 ๋ฌด์์ ํ๋๊ฐ?
๊ธฐ์กด์๋ useMemo, useCallback, React.memo ์ ๊ฐ์ API ๋ก ์ง์ ๋ฉ๋ชจ์ด์ ์ด์
ํ์๋ค. ํ์ง๋ง ๊ฐ๋ฐํ๋ค๋ณด๋ฉด ์ด๋ฅผ ์์ด๋ฒ๋ฆด์ ์๋ค.
๋ฆฌ์กํธ ์ปดํ์ผ๋ฌ๋ ์๋์ผ๋ก ์ปดํฌ๋ํธ์ Hooks ๋ด์ ๊ฐ ๋๋ ๊ฐ ๊ทธ๋ฃน์ ๋ฉ๋ชจ์ด์ ์ด์ ํ๋ค. ๋ง์ฝ JS๋ React ๊ท์น์ ์๋ฐํ ๊ฒฝ์ฐ์๋ ํด๋น ์ปดํฌ๋ํธ ๋๋ Hooks๋ฅผ ๊ฑด๋๋ฐ๊ณ ๋ค๋ฅธ ์ฝ๋๋ฅผ ์ปดํ์ผํ๋ค.
์ปดํ์ผ๋ฌ๊ฐ ์ด์ ์ ๋ง์ถ ๋๊ฐ์ง ์ฌ๋ก
- ์ปดํฌ๋ํธ์ ์ฐ์์ ์ธ ๋ฆฌ๋ ๋๋ง ๊ฑด๋๋ฐ๊ธฐ : ๋ถ๋ชจ๋ง์ด ๋ณ๊ฒฝ๋์์ด๋ ์์๊น์ง ์ ๋ถ ๋ฆฌ๋ ๋๋ง๋๋ ๊ฒฝ์ฐ
- React ์ธ๋ถ์์์ ๋น์ฉ์ด ๋ง์ด ๋๋ ๊ณ์ฐ ๊ฑด๋๋ฐ๊ธฐ : ์ปดํฌ๋ํธ ๋ด์์ ํธ์ถํ๋ ๊ฒฝ์ฐ ๋ฉ๋ชจ์ด์ ์ด์ ๋จ
// ํจ์ ์์ฒด๋ ๋ฆฌ์กํธ ์ปดํ์ผ๋ฌ์ ์ํด ๋ฉ๋ชจ์ด์ ์ด์
๋์ง ์์.
function expensiveProcess() { ... }
function Table() {
// ๋ฆฌ์กํธ ์ปดํ์ผ๋ฌ์ ์ํด ์ปดํฌ๋ํธ ๋ด์์ ๋ฉ๋ชจ์ด์ ์ด์
๋จ.
const data = expensiveProcess();
}1.2 ์ปดํ์ผ๋ฌ๊ฐ ๊ฐ์ ํ๋ ๊ฒ
- ์ฌ๋ฐ๋ฅด๊ณ ์๋ฏธ์๋ JS ์ฝ๋๋ก ์์ฑ๋์๋ค (valid, semantic JavaScript.)
- nullable/optional ๊ฐ๊ณผ ์์ฑ์ ์ ๊ทผํ๊ธฐ ์ ์ ๊ทธ ๊ฐ์ด ์ ์๋์ด์๋์ง ํ
์คํธํ๋ค. (TS๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ
strictNullCheck๋ฅผ ํ์ฑํํ์ฌ ์ํํจ) - React ๊ท์น์ ๋ฐ๋ฅธ๋ค.
๋ง์ฝ ๊ท์น์ ์ด๊ธ๋๋ ๊ฒ์ด ์๋ค๋ฉด ๊ทธ ์ปดํฌ๋ํธ๋ ์ปดํ์ผ์ ๊ฑด๋๋ด๋ค.
2. ๋ด๋ถ ์ฝ๋
- ์ปดํ์ผ ์ ๋ฆฌ์กํธ ์ฝ๋
function FriendList({ friends }) {
const onlineCount = useFriendOnlineCount();
if (friends.length === 0) {
return <NoFriends />;
}
return (
<div>
<span>{onlineCount} online</span>
{friends.map((friend) => (
<FriendListCard key={friend.id} friend={friend} />
))}
<MessageButton />
</div>
);
}- ์ปดํ์ผ ํ ์ฝ๋
- ์์กด์ฑ์ด ์๋ ํ์ ์ปดํฌ๋ํธ๋
Symbol.for("react.memo_cache_sentinel")์ด๋ฉด ๋ ๋๋ง, ์๋๋ฉด ์ด์ ์ ๋ง๋ ๊ฐ ์ฌ์ฉ - ์์กด์ฑ์ด ์๋ ์ปดํฌ๋ํธ๋ ์์กดํ๋ ๊ฐ๋ค์ด ์ด์ ๊ณผ ๊ฐ์์ง ๋ค๋ฅธ์ง์๋ฐ๋ผ ๋์ ๊ตฌ๋ถ
- ์์กด์ฑ์ด ์๋ ํ์ ์ปดํฌ๋ํธ๋
function FriendList(t0) {
const $ = _c(9);
const { friends } = t0;
const onlineCount = useFriendOnlineCount();
if (friends.length === 0) {
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t1 = <NoFriends />;
$[0] = t1;
} else {
t1 = $[0];
}
return t1;
}
let t1;
if ($[1] !== onlineCount) {
t1 = <span>{onlineCount} online</span>;
$[1] = onlineCount;
$[2] = t1;
} else {
t1 = $[2];
}
let t2;
if ($[3] !== friends) {
t2 = friends.map(_temp);
$[3] = friends;
$[4] = t2;
} else {
t2 = $[4];
}
let t3;
if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
t3 = <MessageButton />;
$[5] = t3;
} else {
t3 = $[5];
}
let t4;
if ($[6] !== t1 || $[7] !== t2) {
t4 = (
<div>
{t1}
{t2}
{t3}
</div>
);
$[6] = t1;
$[7] = t2;
$[8] = t4;
} else {
t4 = $[8];
}
return t4;
}
function _temp(friend) {
return <FriendListCard key={friend.id} friend={friend} />;
}
์ปดํ์ผ ํ ์ฝ๋๋ฅผ ๋ณด๋ฉด _c ํจ์์์ ๋ฉ๋ชจ์ด์ ์ด์
์ ๋ด๋นํ๊ณ ์๋๊ฒ์ ํ์ธํ ์ ์๋ค.
- ํ์ํ ์์กด์ฑ๋งํผ์ ์ฌ์ด์ฆ๋ฅผ ๊ฐ์ง ๋ฐฐ์ด ์ํ๋ฅผ ๋ฐํ
- ๊ฐ ์์์ ์ด๊ธฐ๊ฐ์
Symbol.for("react.memo_cache_sentinel")
// compiler/packages/react-compiler-runtimes/src/index.ts
type MemoCache = Array<number | typeof $empty>;
const $empty = Symbol.for('react.memo_cache_sentinel');
/**
* DANGER: this hook is NEVER meant to be called directly!
**/
export function c(size: number) {
return React.useState(() => {
const $ = new Array(size);
for (let ii = 0; ii < size; ii++) {
$[ii] = $empty;
}
// This symbol is added to tell the react devtools that this array is from
// useMemoCache.
// @ts-ignore
$[$empty] = true;
return $;
})[0];
}3. ๊ถ๊ธ์ฆ
- ์ฌ๋ฐ๋ฅด๊ณ ์๋ฏธ์๋ JS ๋ผ๋ ๊ฒ์? (valid, semantic JavaScript.)
- valid : ECMAScript ํ์ค์ ๋ถํฉํ๋ ๋ฌธ๋ฒ๊ณผ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ํ์ฑ ๊ณผ์ ์์ ์๋ฌ๋ ๊ฒฝ๊ณ ๋ฅผ ์ผ์ผํค์ง ์๋ ์ฝ๋
- semantic : ๋ฌธ๋ฒ์ ์ผ๋ก๋ง ๋ง์ถ๋ ๊ฒ์ ๋์ด, ์๋ํ ๋ฐ๋ฅผ ๋ช
ํํ ๋๋ฌ๋ด๊ณ ์์ ๊ฐ๋ฅํ ๋์์ ํ๋๋ก ๊ตฌ์ฑ๋ ์ฝ๋๋ฅผ ์๋ฏธ.
- ex) ๋ณ์๋ ํจ์ ์ฌ์ฉ ์ ์๋๋ ์ค์ฝํ์์ ์ ํจํ๊ฒ ์ฌ์ฉ. ํ์ ๋ณํ ๋ก์ง์ด๋ ๋น๋๊ธฐ ํ๋ฆ์ด ๋ช ํํ๊ฒ ๊ด๋ฆฌ๋จ
- ์ต์ด ๋ก๋ฉ ์ฑ๋ฅ์ ์ํฅ์ ์ค๊น?
- ํฐ ์ํฅ์ด ์๋ค๊ณ ํจ
- ์ ์ ๊ฐ ์ํธ์์ฉ์ ์ฑ๋ฅ์ ์ด๋จ๊น?
- ๋ถํ์ํ ๋ฆฌ๋ ๋๋ง์ ์ค์ด๋ ๋งค์ฐ ๋นจ๋ผ์ง
- ๋ชจ๋ ๋ฆฌ๋ ๋๋ง์ ์ก์์ ๋์ด์ ๊ฐ๋ฐ์๋ ๋ฉ๋ชจ์ด์ ์ด์
์ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋ ๊น?
- ์๋๋ค. ๋ฆฌ์กํธ ์ปดํ์ผ๋ฌ๋ props ๊ฐ ๋ณํ์ง ์์๋๋ง ๋์๋์ง, props ์ค๊ณ๋ฅผ ์๋ชปํ๋ฉด ๋ฆฌ๋ ๋๋ง์ ์ฌ์ ํ ๋ฐ์ํ๊ธฐ์ ์ ๊ฒฝ์ ์จ์ผํ๋ค.
- ์ฐธ์กฐ๋ฅผ props๋ก ๋ฃ์์๋ ๋ฆฌ์กํธ ์ปดํ์ผ๋ฌ๋ ์๋ํ์ง ์๋๊ฐ?
- ์๋๋ค. ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํ์๋๋ ์ ์ ํ ์ปดํ์ผ ๋๋ค. ๋ค๋ง ์จ๊ฒจ์ง ์ฐธ์กฐ(๋คํธ์ํฌ ์์ฒญ ๋๋ ๋ค๋ฅธ ๊ณณ์์ ๋ฐ์์จ ์ฐธ์กฐ)์ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๊ฐ ๊ฐ์งํ์ง ๋ชปํ ์ ์๋ค.
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ์์๋?
- ๋ฆฌ์กํธ ์ ์ฅ์ผ๋ก ๋์์ ์์ง ์ ๋ชปํจ