Skip to main content

reactCompiler(实验)

此功能是实验性的,可能会在未来版本中发生变化。

¥This feature is experimental and may change in future releases.

Next.js 15 引入了对 React 编译器 的实验性支持。编译器通过自动优化组件渲染来提高性能。这减少了开发者必须通过 useMemouseCallback 等 API 进行的手动记忆量。

¥Next.js 15 introduced experimental support for the React Compiler. The compiler improves performance by automatically optimizing component rendering. This reduces the amount of manual memoization developers have to do through APIs such as useMemo and useCallback.

要使用它,请升级到 Next.js 15,安装 babel-plugin-react-compiler

¥To use it, upgrade to Next.js 15, install the babel-plugin-react-compiler:

npm install babel-plugin-react-compiler

然后,在 next.config.js 中添加 experimental.reactCompiler 选项:

¥Then, add experimental.reactCompiler option in next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
reactCompiler: true,
},
}

module.exports = nextConfig

或者,你可以将编译器配置为在 "opt-in" 模式下运行,如下所示:

¥Optionally, you can configure the compiler to run in "opt-in" mode as follows:

const nextConfig = {
experimental: {
reactCompiler: {
compilationMode: 'annotation',
},
},
}

module.exports = nextConfig

注意:React Compiler 目前只能通过 Babel 插件在 Next.js 中使用。这将退出 Next.js 的默认 基于 Rust 的编译器,这可能会导致构建时间变慢。我们正在努力支持 React Compiler 作为我们的默认编译器。

¥Note: The React Compiler is currently only possible to use in Next.js through a Babel plugin. This will opt-out of Next.js's default Rust-based compiler, which could result in slower build times. We are working on support for the React Compiler as our default compiler.

详细了解 React 编译器可用的 Next.js 配置选项

¥Learn more about the React Compiler, and the available Next.js config options.