Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel Plugin Class Properties – React Arrow Functions

I am running a React project using npm. After hours of research and experimenting, everywhere says I have to add the following code to my ".babelrc" file, which I do not have in my directory and cannot create:

{
  "plugins": [
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ]
}

But this leads to the following error when I run the code:

ERROR in ./src/components/NavBar/Menu.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/ymoondhra/Desktop/yt-web/src/components/NavBar/Menu.js:
Support for the experimental syntax 'classProperties' isn't currently enabled
 (20:10):
  18 |     }
  19 | 
> 20 |     fast = () => {
     |          ^
  21 |         this.speed = 100;
  22 |     }
  23 | 
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' 
section of your Babel config to enable transformation.
like image 325
Y. Moondhra Avatar asked Jan 27 '23 23:01

Y. Moondhra


1 Answers

Found the solution to my question after 3 hours: –create a file called "babel.config.js" in the root directory. –put this in the file. –rerun the program (i.e. npm start).

module.exports = {
  presets: [ "@babel/preset-env", "@babel/preset-react" ],
  plugins: [ "@babel/plugin-transform-arrow-functions", "@babel/plugin-proposal-    class-properties" ]
}
like image 165
Y. Moondhra Avatar answered Jan 31 '23 21:01

Y. Moondhra