简体   繁体   中英

Support for the experimental syntax 'classProperties' isn't currently enabled error on a React.js

When I am trying to use react-native-vector-icon with a React Project build with react-native-web , I am getting

Support for the experimental syntax 'classProperties' isn't currently enabled

I have tried the following solution but none of those working for me.

  1. Support for the experimental syntax 'classProperties' isn't currently enabled
  2. Babel Plugin Class Properties – React Arrow Functions
  3. "loose": true is not fixing Support for the experimental syntax 'classProperties' isn't currently enabled

My package.json

{
  "name": "react-webiste",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.19.2",
    "fsevents": "^2.1.2",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-native": "^0.62.1",
    "react-native-svg": "^12.1.0",
    "react-native-vector-icons": "^6.6.0",
    "react-native-web": "^0.12.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.0.0",
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@babel/plugin-proposal-class-properties": "^7.8.3"
  }
}

babel.config.js

module.exports = {
  presets: ["@babel/preset-env", "@babel/preset-react"],
  plugins: ["@babel/plugin-proposal-class-properties",{"loose": true}]
};

错误截图链接

Can anyone please suggest me what should I do to fix this issue.

EDIT: It seems like your issue is a current problem with the library owner. The library is built for react-native where users are used to manually transpile. You can find the GitHub project issue here

However, as some users suggest, you could try to use config-overrides.js , adding resolveApp('../node_modules/react-native-vector-icons') to your bundling step like described in this GitHub comment .

In order to use config-overrides , you should migrate to react-app-rewired ( https://github.com/timarney/react-app-rewired ).

I'll paste the config-overrides changes for future reference, kudos to jookovjok , and he's full config-overrides.js :

const appIncludes = [
    ...
    resolveApp('../node_modules/react-native-vector-icons') // <- HERE
    ]
... 
    config.module.rules[2].oneOf[1].options.plugins = [
        ...
        require.resolve('@babel/plugin-proposal-class-properties') // <- HERE
    ]...

Old answer before edit:

Have you pasted correctly the babel.config.js ? It seems to me your plugins declaration is mispelled:

Change the plugins line to: (removing the extra t ):

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

In the general case, all the babel configuration can be passed down from babel.config.js or any other supported formate files. But in my case, babel-loader in the webpack.config.js has its own option as a result of which all configuration were overwritten as described by Babel team on Github Comment.

Secondly, I used metro-react-native-babel-preset instead of @babel/plugin-proposal-class-properties which took care of the babel missing issue.

Here is my Webpack Config after ejecting the application:

   {
       test: /\.(js|mjs)$/,
       exclude: /@babel(?:\/|\\{1,2})runtime/,
       loader: require.resolve('babel-loader'),
       options: {
           babelrc: false,
           configFile: false,
           compact: false,

          // Added the module here
           presets: [
             [
                require.resolve('babel-preset-react-app/dependencies'),
                { helpers: true },
             ],["module:metro-react-native-babel-preset"]
          ],

      // More configuration codes goes here 


 {
         test: /\.(js|mjs|jsx|ts|tsx)$/,
         enforce: 'pre',
         use: [
           {
             options: {
               cache: true,
               formatter: require.resolve('react-dev-utils/eslintFormatter'),
               eslintPath: require.resolve('eslint'),
               resolvePluginsRelativeTo: __dirname,

             },
             loader: require.resolve('eslint-loader'),
           },
         ],
         include:[  //Change here as well
           paths.appSrc,
           path.resolve('node_modules/react-native-vector-icons'),
         ]
       },

Once all these done, react-native-vector-icon will render but nothing will load. For that we need to add the below styles into the App.css or any root style sheet.


/*Import the Icon CSS*/
@font-face {
  font-family: "Entypo";
  src: url("~react-native-vector-icons/Fonts/Entypo.ttf") format("truetype");
}

@font-face {
  font-family: "EvilIcons";
  src: url("~react-native-vector-icons/Fonts/EvilIcons.ttf") format("truetype");
}

@font-face {
  font-family: "FontAwesome";
  src: url("~react-native-vector-icons/Fonts/FontAwesome.ttf")
  format("truetype");
}

@font-face {
  font-family: "fontcustom";
  src: url("~react-native-vector-icons/Fonts/Foundation.ttf") format("truetype");
}

@font-face {
  font-family: "Ionicons";
  src: url("~react-native-vector-icons/Fonts/Ionicons.ttf") format("truetype");
}

@font-face {
  /*font-family: 'MaterialCommunityIcons';*/
  font-family: "Material Design Icons";
  src: url("~react-native-vector-icons/Fonts/MaterialCommunityIcons.ttf")
  format("truetype");
}

@font-face {
  font-family: "MaterialIcons";
  src: url("~react-native-vector-icons/Fonts/MaterialIcons.ttf")
  format("truetype");
}

@font-face {
  font-family: "Octicons";
  src: url("~react-native-vector-icons/Fonts/Octicons.ttf") format("truetype");
}

@font-face {
  font-family: "simple-line-icons";
  src: url("~react-native-vector-icons/Fonts/SimpleLineIcons.ttf")
  format("truetype");
}

@font-face {
  font-family: "Zocial";
  src: url("~react-native-vector-icons/Fonts/Zocial.ttf") format("truetype");
}

I tried a lot of things across multiple days. What finally solved it for me to make classProperties work on react-native-web was following this simple guide for a very popular repo: https://reactnativeelements.com/docs/web_usage/

You have to add the package that causing problems for you (react-native-vector-icons) in the file config-overrides.js :

const path = require('path');
const { override, addBabelPlugins, babelInclude } = require('customize-cra');

module.exports = override(
  ...addBabelPlugins('@babel/plugin-proposal-class-properties'),
  babelInclude([
    path.resolve(__dirname, 'node_modules/react-native-elements'),
    path.resolve(__dirname, 'node_modules/react-native-vector-icons'),
    path.resolve(__dirname, 'node_modules/react-native-ratings'),
    path.resolve(__dirname, 'src'),
  ])
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM