reactjs - 如何将react-native-vector-icons与react-native-web一起使用?

标签 reactjs react-native react-native-web react-native-vector-icons

我已经使用 react-nativereact-native-web 设置了 monorepo 项目。我正在为 Android、iOS 和 Web 共享相同的代码库。安装react-native-vector-icons后,我在所有三个平台上运行了代码,它在Android和iOS中运行良好,但在Web中运行不佳。 在网络中,它如下所示:

enter image description here

我已经按照描述设置了 Webpack here

下面是我的Webpack配置config-overrides.js:

const fs = require('fs');
const path = require('path');
const webpack = require('webpack');

const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);

// our packages that will now be included in the CRA build step
const appIncludes = [
    resolveApp('src'),
    resolveApp('../components/src'),
];
//below is the config for react-native-vector-icons
const ttf = {
    test: /\.ttf$/,
    loader: "file-loader",
    include: path.resolve(__dirname, "../../node_modules/react-native-vector-icons"),
};
module.exports = function override(config, env) {
    // allow importing from outside of src folder
    config.resolve.plugins = config.resolve.plugins.filter(
        plugin => plugin.constructor.name !== 'ModuleScopePlugin'
    );
    config.module.rules[0].include = appIncludes;
    config.module.rules[1] = ttf; //add the react-native-vector-icons here
    config.module.rules[2].oneOf[1].include = appIncludes;
    config.module.rules[2].oneOf[1].options.plugins = [
        require.resolve('babel-plugin-react-native-web'),
    ].concat(config.module.rules[2].oneOf[1].options.plugins);
    config.module.rules = config.module.rules.filter(Boolean);
    config.plugins.push(
        new webpack.DefinePlugin({__DEV__: env !== 'production'})
    );
    return config
};

下面是我的 App.js 文件中 react-native-vector-icons 的使用

import Icon from 'react-native-vector-icons/dist/FontAwesome';

<Icon name="glass" size={24}/>

我不知道为什么图标没有加载或者我错过了配置。 请帮我。 预先感谢您。

最佳答案

好的。因此,假设您使用 nextjs 和 react-native-web。我将逐步详细介绍如何使其在网络上运行。

安装所有这些软件包 yarn add react-native-vector-icons react-native-svg next-compose-plugins next-transpile-modules

更新您的 next.config.js

const withPlugins = require('next-compose-plugins');
const withTM = require('next-transpile-modules')(['react-native-vector-icons', 'react-native-svg']);

module.exports = withPlugins([withTM], {
  images: {
    disableStaticImages: true,
  },
  typescript: {
    ignoreBuildErrors: true,
  },
  webpack: (config) => {
    config.resolve.alias = {
      ...(config.resolve.alias || {}),
      // Transform all direct `react-native` imports to `react-native-web`
      'react-native$': 'react-native-web',
    }
    config.resolve.extensions = [
      '.web.js',
      '.web.ts',
      '.web.tsx',
      ...config.resolve.extensions,
    ]
    config.module.rules.push(
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: [ 'url-loader?limit=10000', 'img-loader' ]
      },
    );
    return config
  },
});

转到node_modules/react-native-vector-icons/Fonts并复制您需要的所有字体并将它们粘贴到您的公共(public)文件夹中,就像将自定义字体添加到项目中一样,您需要包含所有这些字体就像你的 style.css 中的那样

@font-face {
    src: url(Ionicons.ttf);
    font-family: "Ionicons";
}

@font-face {
    src: url(FontAwesome.ttf);
    font-family: "FontAwesome";
}

@font-face {
    src: url(MaterialIcons.ttf);
    font-family: "MaterialIcons";
}

@font-face {
    src: url(Feather.ttf);
    font-family: "Feather";
}

@font-face {
    src: url(MaterialCommunityIcons.ttf);
    font-family: "MaterialCommunityIcons";
}

在您的 _document.js 文件中,确保包含 style.css 文件,如下所示

        <Head>
          <link href="/fonts/style.css" rel="stylesheet"/>
        </Head>

最后导入它 import Icon from 'react-native-vector-icons/MaterialCommunityIcons';

并设置它 <Icon name="google-play" size={30} color="#900" />

瞧!

关于reactjs - 如何将react-native-vector-icons与react-native-web一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60263461/

相关文章:

react-native - react native 抽屉导航 : Drawer not closing when pressing an overlay

react-native build android failed mismatch react-native模块编译版本和运行时版本错误

reactjs - 具有继承的对象分配在 typescript 中不起作用

javascript - 在 react 应用程序中获取文档滚动百分比的最佳方法是什么?

reactjs - 如何使用 Reactjs 获取 CSV 数据并存储到状态?

javascript - 是否可以在 React Native 中使用 Animated 为 Modal 设置动画?

javascript - 如何在 const 方法中返回

android - 即使应用程序在后台,React Native Android 应用程序每小时消耗 30mA

react-native - 如何在 react-native 中创建线性渐变(在图像顶部和应用栏下方)

javascript - 添加 react 原生矢量图标以 react 原生与 react 原生网络时出错