guide of how to use react-native-vector-icons on web with webpack and react-app-rewired
- yarn add react-native-vector-icons react-native-web
- yarn add react-app-rewired -D
- update package.json scripts (before
react-scripts start/build/test
)
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --env=jsdom",
- create a new file named config-overrides.js add it to the root of the project
const path = require('path');
module.exports = function override(config) {
config.module.rules.push({
test: /\.ttf$/,
loader: 'file-loader',
include: path.resolve(__dirname, './static/media/[name].[ext]'),
});
return config;
};
- create a new file named icons.js and add it wherever you like
/* eslint-disable camelcase */
import FontAwesome_ttf from 'react-native-vector-icons/Fonts/FontAwesome.ttf';
import Entypo_ttf from 'react-native-vector-icons/Fonts/Entypo.ttf';
const IconsCSS = `
@font-face {
src: url(${FontAwesome_ttf});
font-family: FontAwesome;
}
@font-face {
src: url(${Entypo_ttf});
font-family: Entypo;
}
`;
const style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) style.styleSheet.cssText = IconsCSS;
else style.appendChild(document.createTextNode(IconsCSS));
document.head.appendChild(style);
-
import icons.js file in your index.js file like that
import './utils/icons'; // for append icons css
-
use - galery of icons (https://oblador.github.io/react-native-vector-icons/)
import FontAwesomeIcon from 'react-native-vector-icons/dist/FontAwesome';
import EntypoIcon from 'react-native-vector-icons/dist/Entypo';
<FontAwesomeIcon name="pied-piper-alt" size={30} color="#010" />
<EntypoIcon name="bug" size={30} color="#404" />
thank you very much. I was struggling with it from hours