Languages
[Edit]
EN

React - convert SVG graphics to React SVG Components

6 points
Created by:
Kate_C
21370

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

Quick solition (run following command):

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

 

Read next sections to know:

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

1. Instalation example

    Simple instruction how to install SVGR globaly:

    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
    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