React Compiler: what does it do and what's new

June 9, 20243 min read
React Compiler: what does it do and what's new

What is the React Compiler?

The React Compiler is a experimental compiler that has been open sources to the community in order to receive feedback and ideas. As you can imagine this means it is not production ready yet. However, it is a great way to get a feel for what this compiler of React does and can do. Keep in mind that this experimental compiler needs React 19 to work.

What does the React Compiler do?

The main purpose of the React Compiler is to memoize your application and make it thus faster and more efficient. In a lot of cases and projects the current way of working is to add common API's to your application and then use them in your components. API's like `useMemo`, `useCallback`, and `React.memo` are great in this aspect but hard to understand and maintain for a great part of the community, and let's be honest, they are not always the best solution. These API's are also often used in a incorrect way or are forgotten to be used. Thus the React team created the React Compiler to help you with this.

Keep in mind that if your project is already correctly memoized that you might not see that any or little performance improvements.

What does the compiler assume?

In the React team their own words the compiler assumes that your code:

  • Follows the Rules of React
  • Is correct and valid semantic JavaScript
  • Tests that nullable/optional values and properties are defined before accessing them (for example, by enabling `strictNullChecks` if using TypeScript), i.e., `if (object.nullableProperty) { object.nullableProperty.foo }` or with optional-chaining `object.nullableProperty?.foo` Follows the Rules of React

How can I experiment with the React Compiler?

You can check if your codebase is compatible with the React Compiler by running the following command:

npx react-compiler-healthcheck@latest

What this script will do is check how many components can be successfully optimized, if `<StrictMode>` is used, and if incompatible libraries are used.

An example of n output can be seen below:

❯ npx react-compiler-healthcheck@latest

Successfully compiled 41 out of 41 components. StrictMode usage found. Found no usage of incompatible libraries.

There is also an eslint plugin available that you can use to check your codebase for compatibility with the React Compiler. You can install it with the following command:

npm install eslint-plugin-react-compiler

And then add it to your eslint config file:

This plugin is very helpful as it will display all the violations that can occur in your codebase with the React Compiler. If you see an error it means that the compiler has skipped optimization of that component, this will not block your application from running as the compiler will recover and continue.

As I mostly write about Next.js I will not go into detail on how to enable it for your project that does not use Next.js. But you can read onward from [here](https://react.dev/learn/react-compiler#using-the-compiler-effectively) in order to set it up for your project.

For Next.js however to setup to use the compiler do the following:

npm install next@canary babel-plugin-react-compiler
Then add the following to your `next.config.js` file:
module.exports = nextConfig;

What tells me that my code has been optimized?

The [React Devtools](https://react.dev/learn/react-developer-tools) version 5 has built-in support for React Compiler and will display a “Memo ✨” badge next to components that have been optimized by the compiler.

Read more posts

The new Form Component from Next.js 15

The new Form Component from Next.js 15

October 28, 20242 min read
Next.js 15 has just released and it has come with a new Form Component. This new Form Component is especially useful for forms that redirect to a new url for example when you try to search for blog posts and that redirects to a page that displays all the blog posts with that specific query.
Astro 3.0 has made waves in the web development community

Astro 3.0 has made waves in the web development community

September 10, 20245 min read
Astro 3.0 has made waves in the web development community as the first major framework to support the View Transitions API. This groundbreaking feature allows for seamless and visually appealing transitions between different states or views in web applications. With just a few lines of code, developers can easily incorporate fade, slide, morph, and persist animations across page navigation, creating a more immersive user experience.
Testing Next.js Apps: Best Practices & Tools

Testing Next.js Apps: Best Practices & Tools

August 15, 20247 min read
Testing is an essential part of the software development process, ensuring that applications function as expected and remain stable over time. In this blog, we will explore the best practices for testing Next.js applications using popular testing tools like Jest, Cypress, Playwright, and React Testing Library. By following these guidelines, you can maximize your confidence in your code, catch bugs early, and deliver high-quality software to your users.
Refactoring ContentLayer to Velite

Refactoring ContentLayer to Velite

June 3, 20245 min read
As you might have heard already contentlayer is in a bit of a pickle with the amount of support the project is getting, you can read more about it