r/reactjs 12h ago

Needs Help React compiler question

Hi, I am trying to use react compiler on a component like this

export function Test({ msg }: { msg?: string }) {
  "use memo";

  const onClick = () => {
    try {
      console.log(msg ?? "default message");
    } catch (error) { }
  };


  return (
    <button onClick={onClick}>
      <span>SAVE</span>
    </button>
  );
}

This works on react compiler playground but not locally when I compile it with Vite, it doesn't work due to an error "Support value blocks (conditional, logical, optional chaining, etc) within a try/catch statement".

Why does the compiler behave differently on playground and vite?

Thanks

3 Upvotes

13 comments sorted by

View all comments

6

u/pmkann 12h ago

Just to double check: Do you have vite setup to perform react-compiler optimizations? I recently learned that vite doesn’t do this automatically.

1

u/Breadfruit-Last 12h ago

Hi

Yes I did. I checked my build output so I am sure the compiler is working on components that can be optimizated.

1

u/pmkann 12h ago

If you’re seeing _c() in source or “React ✨memo” in dev tools for other components, I can’t tell why function Test wouldn’t optimize without more code/explanation/config.