ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฏ memo๋ ๋ฌด์์ธ๊ฐ์?
**memo**๋ React์์ ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ ๋๋งํ์ง ์๋๋ก ๋ฉ๋ชจ(๊ธฐ์ต)ํด ๋๋ ๊ธฐ๋ฅ์ด์์.
์ฝ๊ฒ ๋งํด์, ๋ถํ์ํ ๋ ๋๋ง์ ๋ฐฉ์งํด์ ์ฑ๋ฅ์ ์ต์ ํํด์ฃผ๋ ๋๊ตฌ๋ผ๊ณ ์๊ฐํ๋ฉด ๋ผ์.
๐ ๋น์ ๋ก ์ดํดํ๊ธฐ: ํผ์ ๋ฐฐ๋ฌ์ ์ด์ผ๊ธฐ
์ฌ๋ฌ๋ถ์ด ํผ์ ๋ฐฐ๋ฌ์์ด๋ผ๊ณ ์์ํด ๋ณด์ธ์. ๐
- ๊ธฐ๋ณธ React ์ปดํฌ๋ํธ (๋ฉ๋ชจ๊ฐ ์๋ ๊ฒฝ์ฐ)
- ๋งค์ผ ๊ฐ์ ์ํํธ์ ํผ์๋ฅผ ๋ฐฐ๋ฌํ๋๋ฐ, ๊ทธ ์ง์ ์ด๋ค ํผ์๋ฅผ ์ฃผ๋ฌธํ๋์ง ๋งค๋ฒ ๋ฌผ์ด๋ณธ๋ค๊ณ ์๊ฐํด ๋ณด์ธ์.
- "์ด๋ค ํผ์ ์ฃผ๋ฌธํ์ จ์ฃ ?" → "๋ง๋ฅด๊ฒ๋ฆฌํ์!"
- ๋นํจ์จ์ ์ด์ฃ ? ๋๊ฐ์ ๊ฑธ ๋ฌผ์ด๋ณด๊ณ ์ผ์ ๋ ๋ง์ด ํ๋ ์ ์ด์์.
- React.memo (๋ฉ๋ชจ๊ฐ ์๋ ๊ฒฝ์ฐ)
- "์ง๋๋ฒ์๋ ๋ง๋ฅด๊ฒ๋ฆฌํ ํผ์๋ฅผ ์ฃผ๋ฌธํ์ผ๋๊น ์ด๋ฒ์๋ ๋๊ฐ์ ๊ฑฐ์ผ"๋ผ๊ณ **๊ธฐ์ต(๋ฉ๋ชจ)**ํด ๋๋ฉด ๋ฉ๋๋ค.
- ์ฃผ๋ฌธ์ด ๋ฌ๋ผ์ง์ง ์์๋ค๋ฉด ๋ค์ ๋ฌผ์ด๋ณผ ํ์๊ฐ ์์ด์!
React์ **memo**๋ ๋ง์ฐฌ๊ฐ์ง์์.
์ปดํฌ๋ํธ์ props(์ฃผ๋ฌธ)๊ฐ ๋ฐ๋์ง ์์๋ค๋ฉด, ๋ค์ ๋ ๋๋งํ์ง ์๊ณ ์ด์ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ์ตํด๋ก๋๋ค.
๐ ๏ธ React.memo ์ฌ์ฉ๋ฒ
memo๋ฅผ ์ฌ์ฉํ๋ฉด ์ปดํฌ๋ํธ๊ฐ ๋ถํ์ํ๊ฒ ๋ค์ ๋ ๋๋ง๋๋ ๊ฒ์ ๋ง์์ค๋๋ค.
ํ๋ฒ ์์ ๋ฅผ ๋ณผ๊ฒ์!
1. ๊ธฐ๋ณธ ์์
๊ธฐ๋ณธ ์ปดํฌ๋ํธ
import React from 'react';
function MyComponent({ name }) {
console.log('MyComponent ๋ ๋๋ง'); // ๋ ๋๋ง ํ์ธ์ฉ
return <p>์๋
ํ์ธ์, {name}๋!</p>;
}
export default MyComponent;
๋ถ๋ชจ ์ปดํฌ๋ํธ
import React, { useState } from 'react';
import MyComponent from './MyComponent';
function ParentComponent() {
const [count, setCount] = useState(0);
return (
<div>
<h1>์นด์ดํธ: {count}</h1>
<button onClick={() => setCount(count + 1)}>์นด์ดํธ ์ฆ๊ฐ</button>
<MyComponent name="ํ๊ธธ๋" />
</div>
);
}
export default ParentComponent;
๐ ์ด ์ฝ๋์ ๋ฌธ์ ์
- ๋ฒํผ์ ํด๋ฆญํ๋ฉด count๊ฐ ๋ฐ๋๋ฉด์ **ParentComponent**๊ฐ ๋ค์ ๋ ๋๋ง๋ฉ๋๋ค.
- **MyComponent**์ name์ ๋ฐ๋์ง ์์์ง๋ง, ๋ถ๋ชจ๊ฐ ๋ค์ ๋ ๋๋ง๋๋ฉด์ MyComponent๋ ๋ถํ์ํ๊ฒ ๋ค์ ๋ ๋๋ง๋ฉ๋๋ค.
2. React.memo๋ก ์ต์ ํํ๊ธฐ
์ด์ React.memo๋ฅผ ์ฌ์ฉํด์ MyComponent๊ฐ ๋ถํ์ํ๊ฒ ๋ ๋๋ง๋์ง ์๋๋ก ๋ง์๋ด ์๋ค.
React.memo ์ ์ฉ
import React from 'react';
// React.memo๋ฅผ ์ฌ์ฉํด์ MyComponent๋ฅผ ๋ฉ๋ชจ์ด์ ์ด์
const MyComponent = React.memo(function MyComponent({ name }) {
console.log('MyComponent ๋ ๋๋ง'); // ๋ ๋๋ง ํ์ธ์ฉ
return <p>์๋
ํ์ธ์, {name}๋!</p>;
});
export default MyComponent;
3. ์ต์ ํ๋ ๊ฒฐ๊ณผ
- count๊ฐ ์ฆ๊ฐํ ๋ **ParentComponent**๋ง ๋ค์ ๋ ๋๋ง๋ฉ๋๋ค.
- MyComponent์ name์ด ๋ฐ๋์ง ์์์ผ๋ฏ๋ก MyComponent๋ ๋ค์ ๋ ๋๋ง๋์ง ์์ต๋๋ค.
- ์ฝ์์ **MyComponent ๋ ๋๋ง**์ด ๋ ์ด์ ์ถ๋ ฅ๋์ง ์๋ ๊ฑธ ํ์ธํ ์ ์์ด์. ๐
๐ React.memo์ ๋์ ์๋ฆฌ
React.memo๋ props๊ฐ ๋ณ๊ฒฝ๋์ง ์์ผ๋ฉด ์ปดํฌ๋ํธ๋ฅผ ๋ค์ ๋ ๋๋งํ์ง ์์ต๋๋ค.
- **"props๊ฐ ์ด์ ๊ณผ ๊ฐ์ ๊ฐ์ธ์ง ํ์ธํ๊ณ ๊ฐ์ผ๋ฉด ์ฌ์ฌ์ฉ"**ํ๋ ์๋ฆฌ์์.
๐ React.memo๋ฅผ ์ฌ์ฉํ๋ฉด ์ข์ ์ํฉ
- ํฐ ์ปดํฌ๋ํธ
- ์ปดํฌ๋ํธ๊ฐ ํฌ๊ณ ๋ณต์กํ ์๋ก ๋ ๋๋ง ๋น์ฉ์ด ๋์์ง๋๋ค.
- memo๋ฅผ ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ์ ํฌ๊ฒ ๊ฐ์ ํ ์ ์์ด์.
- ๋ณ๊ฒฝ๋์ง ์๋ props
- ๋ถ๋ชจ ์ปดํฌ๋ํธ๊ฐ ๋ฆฌ๋ ๋๋ง๋๋๋ผ๋ props ๊ฐ์ด ๋ฐ๋์ง ์๋ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ฉด ์ข์ต๋๋ค.
- ์์ฃผ ๋ ๋๋ง๋๋ ์ปดํฌ๋ํธ
- ๋น๋ฒํ๊ฒ ๋ ๋๋ง๋๋ ์ปดํฌ๋ํธ์ memo๋ฅผ ์ ์ฉํ๋ฉด ๋ถํ์ํ ๋ ๋๋ง์ ์ค์ผ ์ ์์ต๋๋ค.
โ ๏ธ ์ฃผ์ํ ์
- props๊ฐ ์์ฃผ ๋ฐ๋๋ฉด memo์ ํจ๊ณผ๊ฐ ์์ด์.
- React.memo๋ props๊ฐ ๋ฐ๋์ง ์๋ ๊ฒฝ์ฐ์๋ง ํจ๊ณผ์ ์ ๋๋ค.
- ๊ฐ์ฒด๋ ํจ์ props
- ๊ฐ์ฒด๋ ํจ์๋ฅผ props๋ก ์ ๋ฌํ๋ฉด ์ฐธ์กฐ๊ฐ์ด ๋งค๋ฒ ๋ฌ๋ผ์ง๊ธฐ ๋๋ฌธ์ memo๊ฐ ๋ ๋๋ง์ ๋ง์ง ๋ชปํ ์ ์์ด์.
- ์ด๋ด ๋ useMemo๋ useCallback์ ํจ๊ป ์ฌ์ฉํ๋ฉด ์ข์์.
๐ ํ ์ค ์์ฝ
**React.memo**๋ ์ปดํฌ๋ํธ์ props๊ฐ ๋ฐ๋์ง ์์ผ๋ฉด ๋ ๋๋ง์ ๊ฑด๋๋ฐ์ด์ ์ฑ๋ฅ์ ์ต์ ํํด์ฃผ๋ ๊ธฐ๋ฅ์ด์์.
๐ ๋ง๋ฌด๋ฆฌ
React.memo๋ ์ปดํฌ๋ํธ๋ฅผ ๊ธฐ์ตํด์ ๋ถํ์ํ ๋ ๋๋ง์ ๋ง์์ฃผ๋ ๋๋ํ ๋๊ตฌ์์!
ํนํ ํฐ ํ๋ก์ ํธ๋ ์ฑ๋ฅ ์ต์ ํ๊ฐ ํ์ํ ๋ ์์ฃผ ์ ์ฉํฉ๋๋ค.
'web > react' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
use (0) | 2024.12.16 |
---|---|
startTransition (0) | 2024.12.16 |
lazy (0) | 2024.12.16 |
forwardRef (0) | 2024.12.16 |
createContext (0) | 2024.12.16 |
- Total
- Today
- Yesterday
- zustand
- ๋ฒ ํ
- flushsync
- ์คํ์
- ์ํ๊ด๋ฆฌ
- prefetchdns
- react hook
- usedeferredvalue
- react dom
- ์ ์ฅ์
- dns-prefetch
- preconnect
- Nextjs
- Store
- image pull
- useinsertioneffect
- Props
- useRef
- react
- react dom hook
- finddomnode
- useid
- experimental_taintobjectreference
- private regisirty
- useformstatus
- component
- state
- ์ธ๋ถํฐํธ
- experimental_taintuniquevalue
- experimental
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |