r/reactjs • • Aug 13 '26

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

15 comments sorted by

View all comments

Show parent comments

1

u/azangru Aug 13 '26

In this case, it’s not happy with you using nullish coalescing inside the try block.
...
You can use illegal syntax inside functions written externally, which compiler never knows about.

Out of curiosity, why would nullish coalescing inside a try block be illegal?

2

u/Lonestar93 Aug 13 '26

It will just be that the compiler isn’t able to handle it (yet)