3

I am including SVG as reusable component on my react application. Sample of SVG component is as follows

import React from 'react';

export default function IconComponent(): JSX.Element {
    const svg = `
    <svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M36.4811 26.4927H29.7345C26.5728 26.4927 23.9995 23.921 23.9978 20.761C23.9978 17.5977 26.5711 15.0243 29.7345 15.0227H36.4811C37.1711 15.0227 37.7311 15.5827 37.7311 16.2727C37.7311 16.9627 37.1711 17.5227 36.4811 17.5227H29.7345C27.9495 17.5243 26.4978 18.976 26.4978 20.7593C26.4978 22.541 27.9511 23.9927 29.7345 23.9927H36.4811C37.1711 23.9927 37.7311 24.5527 37.7311 25.2427C37.7311 25.9327 37.1711 26.4927 36.4811 26.4927Z" fill="#0C1727"/>
</svg> `;

    return (
        <span
            className="icon icon-component"
            dangerouslySetInnerHTML={{ __html: svg }}
        />
    );
}


On page level, I am importing IconComponent and reusing it. Is there any other best way to include reusable SVGs in React pages which improves performance/page load?

user521024
  • 521
  • 2
  • 7
  • 29

4 Answers4

6

You can create a static file sprites.svg in public folder and define your svg in symbol tag with specific id

<svg
    display="none"
    width="0"
    height="0"
    version="1.1"
    xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink"
>
   <defs>
       <symbol viewBox="0 0 40 40" fill="none" id="your_icon_id">
           <path fill-rule="evenodd" clip-rule="evenodd" d="M36.4811 26.4927H29.7345C26.5728 26.4927 23.9995 23.921 23.9978 20.761C23.9978 17.5977 26.5711 15.0243 29.7345 15.0227H36.4811C37.1711 15.0227 37.7311 15.5827 37.7311 16.2727C37.7311 16.9627 37.1711 17.5227 36.4811 17.5227H29.7345C27.9495 17.5243 26.4978 18.976 26.4978 20.7593C26.4978 22.541 27.9511 23.9927 29.7345 23.9927H36.4811C37.1711 23.9927 37.7311 24.5527 37.7311 25.2427C37.7311 25.9327 37.1711 26.4927 36.4811 26.4927Z" fill="#0C1727"/>
       </symbol>
   </defs>
</svg>

and you can use it by call its id

const SvgIcon = ({ name, className, ...props }) => (
    <svg className={className} {...props}>
        <use xlinkHref={`/static/images/sprites.svg#${name}`} />
    </svg>
);

<SvgIcon name="your_icon_id" width={40} height={40}/>

with this way, your bundle javascript size will decrease because it have no svg code

P/s: You can use svgo for optimizing SVG sprites files

iamhuynq
  • 5,357
  • 1
  • 13
  • 36
  • 1
    I do use this implementation but my concern is won't this Increase the bundle size? Since the svg icon is being used as a component and the code will be added in js file of the build? – Bhushan Patil Aug 29 '22 at 07:19
  • @BhushanPatil it will not increase the bundle size because your file have no svg code, Your svg will be sprites image and will load separately with js code. if size of sprites svg is large, you can see svg icon will displayed after the page appear – iamhuynq Aug 30 '22 at 10:06
3

1. SVG with webpack

command line

$ yarn add -D @svgr/webpack

webpack.config.js

const webpack = require('webpack');

module.exports = {
  entry: './src/index.js',
  module: {
    rules: [
      //...
      {
        test: /\.svg$/,
        use: ['@svgr/webpack'],
      },
    ],
  },
  //...
};

then

import Star from 'star_icon.svg';

<Star />

2. SVG as component

import { ReactComponent as Star } from 'star_icon.svg';

<Star />
<Star width="64" height="64" fill="yellow" />

also you can use props like this.

3. SVG as path

import Star from 'star_icon.svg';

<img src={Star} />

I hope these codes will help you. Personally, I recommend the second method.

CrowRish
  • 174
  • 6
  • 1
    I was on this for almost an hour trying to figure out "symbols" and "use" tags--sometimes the best answers are the simplest. Thanks! – cr4z Mar 22 '23 at 19:42
  • 1
    The second method seems the most preferable to me too but it requires installing SVGR plugin (https://github.com/gregberge/svgr , https://www.npmjs.com/package/@svgr/webpack or https://www.gatsbyjs.com/plugins/gatsby-plugin-svgr/ if you need it in Gatsby) – 1GR3 Jun 19 '23 at 08:36
0

The best practice will be to keep an assets folder inside the source folder and export your icons and create functions for every SVG and return SVG values and do not use dangerous HTML which is not the best practice.

export function DropDownIcon(){ return( <>Your svg value here </> ) }

jeevandza
  • 3
  • 2
0

Avoid adding the raw icon directly to your component. Assume that you want to change the svg icon; you must update the raw icon. This approach is considered as bad practice

Instead, I suggest using SVG icons in React by importing them as components. Follow these steps:

  1. Create an "assets" folder within your project
  2. Place the SVG icon files inside the "assets" folder
  3. In your component, import the icon as follows:
import Icon from "/assert/icon.svg"
import React from "react"
export default function IconComponent(): JSX.Element {
    return (
        <img src={Icon} alt=""/>
    );
}

In the case that you're using the Typescript, you will face the issue according the import icon. Typescript leaves you a message:

TS2307: Cannot find module '/assert/icon.svg' or its corresponding type declarations.

Or transpiler says: [ts] cannot find module '/assert/icon.svg'.

In general, you'll be unable to import svg icon.

This issue has been reported numerous times. If you have a moment, please take a few minutes to review the following links::

Unable to import svg files in typescript

https://duncanleung.com/typescript-module-declearation-svg-img-assets/

Cannot import SVG file into react

typescript cannot find module when import svg file

To avoid this issue, follow the step:

  • Create the @types/assets/index.d.ts and handle the code:
declare module "*.svg";

Hopefully this solution works in your project.

greeneley
  • 87
  • 7