Languages
[Edit]
EN

React - convert SVG graphics to React SVG Components

9 points
Created by:
lena
714

This article shows how to convert SVG graphics to React SVG Components under Linux. The article shows how to use svgr tool from the command line.

Quick solution (run following command):

npx @svgr/cli --icon input-image.svg > output-image.js

 

Read the next sections to know:

  1. how to install SVGR with npm,
  2. what @svgr/cli tool does with file,
  3. useful resources.

1. Installation example

    Simple instruction how to install SVGR globally:

    sudo npm i -g @svgr/cli

    Note: you can install it locally in project as development tool with npm install @svgr/cli --save-dev.

    Example installation output:

    /usr/local/bin/svgr -> /usr/local/lib/node_modules/@svgr/cli/bin/svgr
    + @svgr/cli@5.5.0
    added 144 packages from 116 contributors in 10.843s

    2. Usage example

    SVG image conversion to React componet can be made with following command:

    npx @svgr/cli --icon icon.svg > icon.js

    Example input (icon.svg file):

    <svg
         viewBox="0 0 283.5 226.8"
         xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink"
    >
      <!-- some svg elements here... -->
    </svg>

    Example output (icon.js file):

    import * as React from "react";
    
    function SvgIcon(props) {
      return (
        <svg
          xmlns="http://www.w3.org/2000/svg"
          xmlnsXlink="http://www.w3.org/1999/xlink"
          viewBox="0 0 283.5 226.8"
          width="1em"
          height="1em"
          {...props}
        >
          {/* some elements here... */}
        </svg>
      );
    }
    
    export default SvgIcon;

    3. Useful resources

    1. Project Homepage
    2. Online converter - Playground
    3. GitHub Site
    4. NPM JS Page
    Donate to Dirask
    Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
    Join to our subscribers to be up to date with content, news and offers.

    ReactJS

    React - convert SVG graphics to React SVG Components
    Native Advertising
    🚀
    Get your tech brand or product in front of software developers.
    For more information Contact us
    Dirask - we help you to
    solve coding problems.
    Ask question.

    ❤️💻 🙂

    Join