You can use SVGR that allows us to import SVGs into your React applications as components.
You need to add @svgr/webpack
as a dependency and modify the next.config.js
file like this.
next.config.js
:
module.exports = {
webpack(config) {
config.module.rules.push({
test: /.svg$/,
use: ["@svgr/webpack"]
});
return config;
}
};
Then, you can simply import your icons without using ReactComponent
.
Icon.js
:
import React from "react";
import Bollards from './icons/bollards.svg';
import Earthquake from './icons/earthquake.svg';
import Fire from './icons/fire.svg';
import Healthy from './icons/heartbeat.svg';
import Home from './icons/home.svg';
import Planting from './icons/planting.svg';
import Business from './icons/suitcase.svg';
import Travel from './icons/airplane-around-earth.svg';
const iconTypes = {
bollards: Bollards,
earthQuake: Earthquake,
fire: Fire,
healthy: Healthy,
home: Home,
planting: Planting,
business: Business,
travel: Travel
};
const IconComponent = ({ name, ...props }) => {
let Icon = iconTypes[name];
return <Icon {...props} />;
};
export default IconComponent;
Working demo is available on CodeSandbox.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…