javascriptreactjsbabeljscodesandbox

How to use Babel Plugin Proposal Throw Expressions in Codesandbox?


Using React in Codesandbox i tried to throw new Error('Something went wrong'); and i had this error:

enter image description here

I already added this dependency: https://babeljs.io/docs/en/babel-plugin-proposal-throw-expressions and still won't work.

Here's my code

<div className="App">
  <ErrorBoundary>
    <h1>Counter {counter >= 10 ? throw new Error("Over 10!") : null}</h1>
  </ErrorBoundary>
</div>

Solution

  • After adding a dependency in your sandbox you must enable it. To add a Babel dependency add a .babelrc config file. Then add the plugin name into the plugins array.

    {
      "plugins": [
        "transform-runtime",
        "proposal-optional-chaining",
        "proposal-throw-expressions"
      ],
    }
    

    enter image description here

    The configuration files can be added from the config button on the left side.