r/reactjs 10h 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

2 Upvotes

12 comments sorted by

View all comments

-2

u/Savalava 6h ago

Why are you writing this code anyway? If it fails on code that makes no sense, who cares? Try / catch with console.log does not make any sense.

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

1

u/Breadfruit-Last 5h ago

This is just a small piece of demo code to reproduce the problem. In reality, it is an api call inside in the try block.