빌드 타임 전용 도구
자동 메모이제이션
기존 수동 API:
useMemo, useCallback, React.memo컴파일러는:
React 규칙 기반 최적화
“Hook 규칙, 컴포넌트 순수성” 등을 정적으로 분석
규칙 위반이 감지되면:
핵심 포인트:
코드 전체가 100% “규칙을 완벽히 지켜야만” 쓰는 컴파일러가 아니라, 문제 없는 부분만 골라서 최적화하는 방식을 취한다.
이미:
useMemo, useCallback, memo 등을 잘 써서하지만 현실에서는:
컴파일러의 가치는 “사람이 놓치기 쉬운 부분을 자동으로 보정”해준다는 데 있다.
현재 상태:
권장 사용법:
정리하면:
“안정 버전 나오면 쓰겠다”도 충분히 합리적인 선택
다만 지금부터:
# npm
npm install -D babel-plugin-react-compiler@rc eslint-plugin-react-hooks@^6.0.0-rc.1
# yarn
yarn add -D babel-plugin-react-compiler@rc eslint-plugin-react-hooks@^6.0.0-rc.1
babel-plugin-react-compiler@rc
eslint-plugin-react-hooks@^6.0.0-rc.1
React 19로 바로 올릴 수 없다면:
npm install react-compiler-runtime@rc
target 명시// babel.config.js
const ReactCompilerConfig = {
target: '18', // '17' | '18' | '19'
};
module.exports = function () {
return {
plugins: [
['babel-plugin-react-compiler', ReactCompilerConfig],
],
};
};
react-compiler-runtime
컴파일러는 JS의 동적 특성 때문에 모든 위험 케이스를 완벽히 잡지 못한다.
그래서 기존 프로젝트엔 다음 전략이 권장된다.
const ReactCompilerConfig = {
sources: (filename) => {
return filename.indexOf('src/path/to/dir') !== -1;
},
};
sources 필터를 이용해:
src/feature/dashboard)만 컴파일점점 자신이 생기면:
eslint-plugin-react-hooks / eslint-plugin-react-compiler
→ 에디터에서 React 규칙 위반을 표시
이 말은:
하지만:
라이브러리(컴포넌트/훅 패키지)를 만드는 입장이라면:
원칙:
따라서 라이브러리 쪽 패턴은:
라이브러리 유지보수자가:
컴파일된 코드를 npm에 배포
앱 개발자는:
React 17/18 타겟이라면:
라이브러리 쪽에서:
target 설정react-compiler-runtime을 직접 의존성에 추가런타임이 앱의 React 버전에 맞춰 적절히 동작하도록 구성
주의점:
라이브러리 코드는:
반드시:
"use no memo"로 국소적으로 제외npm install babel-plugin-react-compiler@rc
// babel.config.js
const ReactCompilerConfig = { /* ... */ };
module.exports = function () {
return {
plugins: [
['babel-plugin-react-compiler', ReactCompilerConfig], // 반드시 최우선!
// 나머지 플러그인들...
],
};
};
중요: babel-plugin-react-compiler는 가장 먼저 실행되어야 한다.
// vite.config.js
const ReactCompilerConfig = { /* ... */ };
export default defineConfig(() => {
return {
plugins: [
react({
babel: {
plugins: [
['babel-plugin-react-compiler', ReactCompilerConfig],
],
},
}),
],
};
});
npm install vite-plugin-babel
// vite.config.js
import babel from 'vite-plugin-babel';
const ReactCompilerConfig = { /* ... */ };
export default defineConfig({
plugins: [
remix({ /* ... */ }),
babel({
filter: /\.[jt]sx?$/,
babelConfig: {
presets: ['@babel/preset-typescript'], // TS 사용 시
plugins: [
['babel-plugin-react-compiler', ReactCompilerConfig],
],
},
}),
],
});
Next.js
Webpack
Expo / React Native(Metro)
Rspack / Rsbuild
공통 패턴:
어떤 빌드 도구를 쓰든, 결국 Babel 단계에
babel-plugin-react-compiler를 가장 먼저 꽂아 넣는다가 핵심이다.
React DevTools v5.0+ / React Native DevTools:
이걸로:
가능한 원인:
ESLint가 이미 잡은 “React 규칙 위반”
이 경우, 해당 컴포넌트/훅은 컴파일러가 건너뛴 상태
동작이 이상하다면:
JS의 동적 특성 때문에 컴파일러가 놓친 케이스
즉, 정의되지 않은 동작:
이런 경우:
function SuspiciousComponent() {
"use no memo"; // React 컴파일러 최적화에서 제외
// ...
}
문제가 사라지면:
컴파일러가 해당 코드를 잘못 최적화했다는 뜻
이후:
"use no memo" 사용 시 주의점목적:
문제:
권장 사용 패턴:
긴급하게 문제를 회피할 때만 사용
문제 원인 파악 후:
코드가 올바른 JS라는 전제
널/옵셔널 안전성:
널 가능 값에 접근하기 전에 반드시 체크
if (obj.nullableProp) { obj.nullableProp.foo }obj.nullableProp?.fooTS라면 strictNullChecks 활성화 권장
React 규칙 준수:
React 컴파일러의 핵심 포인트:
무엇을 해주는가
useMemo/useCallback 의존도를 줄이고, 놓치는 구석을 보완어떻게 도입하는가
babel-plugin-react-compiler@rc + eslint-plugin-react-hooksreact-compiler-runtime + target 설정으로 지원어떻게 검증하는가
"use no memo"로 국소 제외 후 원인 추적본질적으로 이 도구는 “리렌더 최적화의 책임을 사람에서 컴파일러로 옮기는 시도”다.
대신, 전제 조건(React 규칙, null 안전 등)을 지키는 쪽으로 코드베이스를 정리해 두는 것이 필수다.