This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 구분 | 예제 A | 예제 B | |
|---|---|---|---|
| 캐시 슬롯 | 4개 | 12개 | |
| 번들 크기 | 작음 | 큼 | |
| 값 순환 시 | 매번 재계산 | 캐시 재사용 | |
| 적합한 경우 | 값이 자주 변하지 않을 때 | 값이 반복적으로 순환할 때 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const B = ({ name }: Props) => { | |
| if (name === '권씨') { | |
| return <div>{expensiveCalculation(name)}</div>; | |
| } else if (name === '오씨') { | |
| return <div>{expensiveCalculation(name)}</div>; | |
| } else { | |
| return <div>{expensiveCalculation(name)}</div>; | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Props = { | |
| name: '권씨' | '오씨' | '박씨'; | |
| }; | |
| const A = ({ name }: Props) => { | |
| return <div>{expensiveCalculation(name)}</div>; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App(t0) { | |
| const $ = _c(4); | |
| const { name } = t0; | |
| console.log("Rendering..."); // 최적화에서 제외 | |
| // ... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App({ show }: { show: boolean }) { | |
| if (show) { | |
| const [count, setCount] = useState(0); // 조건부 훅 호출 | |
| return <button onClick={() => setCount(count + 1)}>{count}</button>; | |
| } | |
| return <div>숨겨짐</div>; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App({ name }: { name: string }) { | |
| console.log('Rendering...'); // 사이드 이펙트 | |
| return <div>{expensiveCalculation(name)}</div>; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App({ user }: { user: { name: string } }) { | |
| user.name = user.name.toUpperCase(); // mutation | |
| return <div>{user.name}</div>; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { c as _c } from "react/compiler-runtime"; | |
| const App = (t0) => { | |
| const $ = _c(4); | |
| const { name } = t0; | |
| let t1; | |
| if ($[0] !== name) { | |
| t1 = expensiveCalculation(name); | |
| $[0] = name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function App({ name }: { name: string }) { | |
| return <div>{expensiveCalculation(name)}</div>; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { use } from 'react'; | |
| export default function ThemeProvider(props) { | |
| if (!props.children) { | |
| return null; | |
| } | |
| // 조건부 반환 이후의 코드도 자동으로 메모이제이션 | |
| const theme = mergeTheme(props.theme, use(ThemeContext)); | |
| return ( | |
| <ThemeContext value={theme}> |