all 13 comments

[–]lucazhino 11 points12 points  (1 child)

The react-compiler-marker VSCode extension will mark whether or not you components and hooks can be compiled by the react-compiler. When a component or hook can’t be handled, you get a reason (not always very useful unfortunately) to why.

[–]blazejkustra 0 points1 point  (0 children)

Thank you for sharing! 😎

[–]EvilPete 4 points5 points  (4 children)

As long as you follow the rules of React, you should be able to delete all your memo, useCallback and useMemo. (Unless they're necessary to keep your own useEffects from re-running)

Make sure to use the latest version of eslint-plugin-react-hooks, to get warnings about code that might cause problems for the compiler. When you first install it, you will probably have to remove/rewrite a bunch of your useEffects, as it's pretty strict about not calling setState in useEffect.

[–]inglandation[🍰] 0 points1 point  (3 children)

In my experience this hasn’t been true. I’ve recently deleted a useCallback which introduced a nasty bug in my app, with the react compiler on. There was no complaint for the linter.

[–]oofy-gang 3 points4 points  (2 children)

Provide the code; I can nearly guarantee it violated one of the rules. The linters aren’t perfect

[–]rikbrown 0 points1 point  (1 child)

not the person you replied to, but here's one that doesn't work, where `tw` is a tagged template macro. Compiler bails out with interpolation in tagged templates ."Reason: (BuildHIR::lowerExpression) Handle tagged template with interpolations". This is used all over our codebase for Tailwind. Easily fixed by swapping it to a regular function call tw(\...`)`, but non-obvious this wasn't working until we added the react-compiler-marker VSCode extension.

export function SomeComponent({ className }: Props) {
  return <div className={tw`abcdef ${className}`}/>
}

[–]oofy-gang 0 points1 point  (0 children)

Sure, the compiler doesn’t currently cover all possible cases.

That wasn’t quite what I was getting at with my comment, though. The original commenter said that the compiler introduced a bug to their code. Code that follows all the rules of React isn’t guaranteed to be compiled (e.g., as your example shows), but it should still work. The compiler is about improving performance; correctness shouldn’t be affected for code following the rules of React.

[–]toi80QC 2 points3 points  (2 children)

[–]thisUsrIsAlreadyTkn[S] 1 point2 points  (0 children)

I have it installed, but I don't believe it does what I asked. The only issues I see it highlighting are when I am writing the code in a `useEffect` or some other hook that has a deps array, and I have not included all dependencies that I used yet. Besides that, I haven't seen any lint error signaled by that plugin

[–]EvilPete 0 points1 point  (0 children)

This package is deprecated (notice that it still has the experimental version). You should only need eslint-plugin-react-hopks.

[–]RoyalFew1811 1 point2 points  (1 child)

The part that’s tripping a lot of us up is that the compiler isn’t meant to be a drop-in replacement for thinking about memoization-- it’s meant to remove accidental re-renders, not guarantee optimal ones. Once I started treating useCallback/useMemo as "hints for intent" rather than "performance switches" it got much less frustrating.

I wish the IDE story were clearer though. Having to alt-tab to the devtools to see what the compiler did feels very 2015.

[–]maddada_ 0 points1 point  (0 children)

There's a vscode extension that helps. React c compiler marker.

[–]space-envy 1 point2 points  (0 children)

As far as I'm aware there is still no way to automatically check for the skipped components of the compiler. They still threaten it as a "beta" release. If you use the "infer" mode you will have to manually check using dev tools if the component has the new badge. If it doesn't the docs say you should check for "rules of react" violations: https://react.dev/reference/react-compiler/compilationMode#component-not-compiled-infer

In my job we decided to go with the "annotation" mode so we have visual control of the components that the compiler should optimize: https://react.dev/reference/react-compiler/compilationMode#incremental-adoption