In this document, you will learn how to build a React application using Rsbuild.
You can use create-rsbuild
to create a project with Rsbuild + React. Just execute the following command:
Then select React
when prompted to "Select framework".
To compile React, you need to register the Rsbuild React Plugin. The plugin will automatically add the necessary configuration for React builds.
For example, register in rsbuild.config.ts
:
For projects using Create React App, you can refer to the CRA Migration Guide.
Rsbuild supports convert SVG to React components via SVGR.
If you need to use svgr, you also need to register the SVGR plugin.
Rsbuild uses React's official Fast Refresh capability to perform component hot updates.
Note that React Refresh requires components to be written according to the standards. Otherwise HMR may not work. You can use eslint-plugin-react-refresh for validation.
For example, if the hot update of the React component cannot take effect, or the state of the React component is lost after the hot update, it is usually because your React component uses an anonymous function. In the official practice of React Fast Refresh, it is required that the component cannot be an anonymous function, otherwise the state of the React component cannot be preserved after hot update.
Here are some examples of wrong usage:
The correct usage is to declare a name for each component function:
React Compiler is an experimental compiler introduced in React 19 that can automatically optimize your React code.
Before you start using React Compiler, it's recommended to read the React Compiler documentation to understand the functionality, current state, and usage of the React Compiler.
The steps to use React Compiler in Rsbuild:
react
and react-dom
to 19. If you are unable to upgrade, you can install the extra react-compiler-runtime package which will allow the compiled code to run on versions prior to 19.You can also refer to the example project.
Set the config for React Compiler as follows:
For React 17 and 18 projects, you need to install react-compiler-runtime and specify the target
:
TanStack Router is a fully type-safe React router with built-in data fetching, stale-while revalidate caching and first-class search-param APIs.
TanStack Router provides @tanstack/router-plugin
to integrate with Rsbuild, which provides support for file-based routing. See:
Rsbuild supports compiling styled-components, improve the debugging experience and add server-side rendering support to styled-components.
If you need to use styled-components, We recommend using the @rsbuild/plugin-styled-components.
You can refer to this example: styled-components.
Rsbuild supports compiling Emotion, you can add the following configuration to use:
You can refer to this example: emotion.
You can use styled-jsx through @swc/plugin-styled-jsx:
Please note, you must select the SWC plugin that matches the current version of @swc/core
for SWC to work, see tools.swc.
You can refer to this example: styled-jsx.
Rsbuild supports @vanilla-extract/webpack-plugin. You can add the following config to use vanilla-extract:
You can refer to this example: vanilla-extract.
You can use StyleX via unplugin-stylex:
You can refer to this example: stylex.