【问题标题】:Error while adding react native vector icons to react native with react native web添加反应原生矢量图标以使用反应原生网络反应原生时出错
【发布时间】:2020-06-06 19:50:42
【问题描述】:

我按照这个tutorialreact-native-web 添加到react-native 项目。但 将react-native-vector-icons 添加到项目时出现错误。

./node_modules/react-native-vector-icons/lib/create-icon-set.js
SyntaxError: /home/hamidreza/Desktop/myApp/node_modules/react-native-vector-icons/lib/create-icon-set.js: Support for the experimental syntax 'classProperties' isn't currently enabled (43:22):

  41 | 
  42 |   class Icon extends PureComponent {
> 43 |     static propTypes = {
     |                      ^
  44 |       allowFontScaling: PropTypes.bool,
  45 |       name: IconNamePropType,
  46 |       size: PropTypes.number,

我还将我的babel.config.js 更改为:

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

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    '@babel/plugin-proposal-class-properties'
  ],
};

但还是有问题。

我该怎么办?

【问题讨论】:

    标签: javascript react-native babeljs react-native-web react-native-vector-icons


    【解决方案1】:

    通过添加

    path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
    

    babelLoaderConfiguration 问题已解决。

    最终web/webpack.config.js:

    const path = require('path');
    const webpack = require('webpack');
    
    const appDirectory = path.resolve(__dirname, '../');
    const babelLoaderConfiguration = {
      test: /\.js$/,
      include: [
        path.resolve(appDirectory, 'index.web.js'),
        path.resolve(appDirectory, 'src'),
        path.resolve(appDirectory, 'node_modules/react-native-uncompiled'),
        path.resolve(appDirectory, 'node_modules/react-native-vector-icons'),
        path.resolve(appDirectory, 'node_modules/react-native-elements'),
      ],
      use: {
        loader: 'babel-loader',
        options: {
          cacheDirectory: true,
          presets: ['react-native'],
          plugins: ['react-native-web'],
        },
      },
    };
    
    const imageLoaderConfiguration = {
      test: /\.(gif|jpe?g|png|svg)$/,
      use: {
        loader: 'url-loader',
        options: {
          name: '[name].[ext]',
        },
      },
    };
    
    module.exports = {
      entry: [path.resolve(appDirectory, 'index.web.js')],
    
      output: {
        filename: 'bundle.web.js',
        path: path.resolve(appDirectory, 'dist'),
      },
      module: {
        rules: [babelLoaderConfiguration, imageLoaderConfiguration],
      },
    
      resolve: {
        alias: {
          'react-native$': 'react-native-web',
        },
        extensions: ['.web.js', '.js'],
      },
    };
    

    【讨论】:

    • 嘿,我添加了 webpack.config.js。 package.json 中的脚本使用“web”运行:“react-scripts start”,我使用了相同的教程。如何将其配置为使用我的自定义 webpack 配置,我遇到了同样的错误。您能否进一步扩展您的设置,这将非常有帮助,谢谢。
    • @RishabhBhatia 我只是将web/webpack.config.js 更改为答案。
    • 教程中没有网页文件夹?您是否在根目录中手动创建了一个文件夹 web 并添加了上面的 webpack.config.js 文件? package.json 或 babel.config.js 中的任何更改
    • @RishabhBhatia 关注这个:reactnativeelements.com/docs/web_usage
    • 有效,那是缺失的部分。周末的好开始,我是个快乐的人。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多