Skip to content

Instantly share code, notes, and snippets.

View jaeyoung-kwon's full-sized avatar

jaeyoung-kwon

  • 21:43 (UTC -12:00)
View GitHub Profile
구분 예제 A 예제 B
캐시 슬롯 4개 12개
번들 크기 작음
값 순환 시 매번 재계산 캐시 재사용
적합한 경우 값이 자주 변하지 않을 때 값이 반복적으로 순환할 때
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>;
}
};
type Props = {
name: '권씨' | '오씨' | '박씨';
};
const A = ({ name }: Props) => {
return <div>{expensiveCalculation(name)}</div>;
};
@jaeyoung-kwon
jaeyoung-kwon / App.tsx
Created November 20, 2025 04:40
React Compiler 결과
function App(t0) {
const $ = _c(4);
const { name } = t0;
console.log("Rendering..."); // 최적화에서 제외
// ...
}
function App({ show }: { show: boolean }) {
if (show) {
const [count, setCount] = useState(0); // 조건부 훅 호출
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}
return <div>숨겨짐</div>;
}
function App({ name }: { name: string }) {
console.log('Rendering...'); // 사이드 이펙트
return <div>{expensiveCalculation(name)}</div>;
}
function App({ user }: { user: { name: string } }) {
user.name = user.name.toUpperCase(); // mutation
return <div>{user.name}</div>;
}
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;
function App({ name }: { name: string }) {
return <div>{expensiveCalculation(name)}</div>;
}
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}>