react-inlinesvg vs react-svg vs react-svg-loader vs svg-inline-react vs react-svg-inline
React SVG Libraries
react-inlinesvgreact-svgreact-svg-loadersvg-inline-reactreact-svg-inlineSimilar Packages:
React SVG Libraries

React SVG libraries provide tools for integrating and manipulating SVG (Scalable Vector Graphics) within React applications. These libraries offer various methods for rendering SVGs, whether inline, as components, or through external files, allowing developers to create scalable, resolution-independent graphics that can be styled and animated with CSS and JavaScript. They enhance the flexibility and performance of SVG usage in React projects, making it easier to manage and optimize vector graphics for web applications. react-inlinesvg is a lightweight library for inlining SVGs from external files, allowing for easy styling and manipulation. react-svg provides a simple way to import and use SVGs as React components, supporting both inline and external SVGs. react-svg-inline focuses on rendering SVGs inline within React components, enabling direct manipulation of SVG elements. react-svg-loader is a webpack loader that converts SVG files into React components, allowing for seamless integration and optimization during the build process. svg-inline-react is a small library for inlining SVGs directly in React components, providing a simple API for embedding SVG code.

Npm Package Weekly Downloads Trend
3 Years
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
react-inlinesvg605,5761,302136 kB39 months agoMIT
react-svg207,356877263 kB1410 months agoMIT
react-svg-loader119,900640-487 years agoMIT
svg-inline-react18,063131-164 years agoMIT
react-svg-inline13,985223-37 years agoMIT
Feature Comparison: react-inlinesvg vs react-svg vs react-svg-loader vs svg-inline-react vs react-svg-inline

SVG Integration Method

  • react-inlinesvg:

    react-inlinesvg integrates SVGs by inlining them from external files, allowing for easy styling and manipulation while keeping the SVG code separate from the React component.

  • react-svg:

    react-svg provides a flexible integration method that supports both inline SVGs and external SVG files, allowing developers to choose the best approach for their needs.

  • react-svg-loader:

    react-svg-loader integrates SVGs by converting them into React components during the webpack build process, allowing for seamless integration and optimization.

  • svg-inline-react:

    svg-inline-react integrates SVGs by inlining the SVG code directly within React components, providing a simple and efficient way to embed SVGs.

  • react-svg-inline:

    react-svg-inline focuses on rendering SVGs inline within React components, enabling direct manipulation of the SVG elements and styles.

Styling and Manipulation

  • react-inlinesvg:

    react-inlinesvg allows for easy styling and manipulation of SVGs using CSS and JavaScript, as the SVGs are inlined into the DOM, making them accessible for dynamic changes.

  • react-svg:

    react-svg supports styling and manipulation of SVGs both as components and as inline elements, providing flexibility in how styles are applied.

  • react-svg-loader:

    react-svg-loader allows for styling and manipulation of SVGs as React components, leveraging the full power of React’s props and state for dynamic changes.

  • svg-inline-react:

    svg-inline-react provides basic styling capabilities for inlined SVGs, allowing for simple CSS-based styling within React components.

  • react-svg-inline:

    react-svg-inline enables direct manipulation of SVG elements within the React component, allowing for more granular control over styling and interactivity.

Performance

  • react-inlinesvg:

    react-inlinesvg offers good performance by inlining SVGs only when needed, reducing the initial load time and allowing for efficient rendering of vector graphics.

  • react-svg:

    react-svg performance depends on how SVGs are integrated, with inline SVGs offering better performance for animations and interactions compared to external SVGs.

  • react-svg-loader:

    react-svg-loader enhances performance by converting SVGs into React components at build time, eliminating the need for runtime parsing and allowing for tree-shaking and other optimizations.

  • svg-inline-react:

    svg-inline-react is lightweight and performs well for inlining SVGs directly within React components, minimizing the overhead associated with external SVG files.

  • react-svg-inline:

    react-svg-inline provides efficient rendering of inline SVGs, which can improve performance by reducing the need for external file requests.

Ease of Use

  • react-inlinesvg:

    react-inlinesvg is easy to use, with a simple API for inlining SVGs from external files. It requires minimal setup and allows for quick integration into React projects.

  • react-svg:

    react-svg provides a user-friendly API for importing and using SVGs as React components, making it easy for developers to integrate SVGs into their applications.

  • react-svg-loader:

    react-svg-loader requires some configuration within the webpack build process but provides a seamless experience once set up, allowing for automatic conversion of SVGs to React components.

  • svg-inline-react:

    svg-inline-react is simple to use, with a minimal API for inlining SVGs directly within React components, making it quick to integrate.

  • react-svg-inline:

    react-svg-inline is straightforward to use for rendering SVGs inline within React components, with a simple interface for embedding SVG code.

Code Examples

  • react-inlinesvg:

    Example of inlining an SVG from an external file using react-inlinesvg:

    import InlineSVG from 'react-inlinesvg';
    
    const MyComponent = () => (
      <div>
        <InlineSVG src="/path/to/your.svg" />
      </div>
    );
    
  • react-svg:

    Example of using react-svg to import an SVG as a React component:

    import { ReactSVG } from 'react-svg';
    
    const MyComponent = () => (
      <div>
        <ReactSVG src="/path/to/your.svg" />
      </div>
    );
    
  • react-svg-loader:

    Example of using react-svg-loader with webpack to import an SVG as a React component:

    import MyIcon from './my-icon.svg';
    
    const MyComponent = () => (
      <div>
        <MyIcon />
      </div>
    );
    
  • svg-inline-react:

    Example of inlining an SVG using svg-inline-react:

    import SVGInline from 'svg-inline-react';
    
    const svg = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
      <rect width="100" height="100" fill="blue" />
    </svg>`;
    
    const MyComponent = () => (
      <div>
        <SVGInline svg={svg} />
      </div>
    );
    
  • react-svg-inline:

    Example of rendering an inline SVG using react-svg-inline:

    import SVGInline from 'react-svg-inline';
    
    const svg = `<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
      <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
    </svg>`;
    
    const MyComponent = () => (
      <div>
        <SVGInline svg={svg} />
      </div>
    );
    
How to Choose: react-inlinesvg vs react-svg vs react-svg-loader vs svg-inline-react vs react-svg-inline
  • react-inlinesvg:

    Choose react-inlinesvg if you need to inline SVGs from external files while maintaining the ability to style and manipulate them with CSS and JavaScript. It is ideal for projects where SVGs are stored separately but need to be integrated seamlessly into React components.

  • react-svg:

    Choose react-svg if you want a versatile solution that allows you to use SVGs as React components, whether they are imported directly or loaded from external sources. It is suitable for projects that require flexibility in how SVGs are integrated and styled.

  • react-svg-loader:

    Choose react-svg-loader if you are using webpack and want to convert SVG files into React components automatically during the build process. This is ideal for projects that prioritize build-time optimization and want to manage SVGs as components without manual conversion.

  • svg-inline-react:

    Choose svg-inline-react if you need a lightweight and straightforward library for inlining SVGs directly within React components. It is best for projects that require a simple solution for embedding SVG code without additional dependencies.

  • react-svg-inline:

    Choose react-svg-inline if you need to render SVGs inline within your React components, allowing for direct manipulation of the SVG elements. This is useful for projects that require fine-grained control over the SVG markup and styling.

README for react-inlinesvg

react-inlinesvg

NPM version CI Quality Gate Status Coverage

Load inline, local, or remote SVGs in your React components.

View the demo

Highlights

  • 🏖 Easy to use: Just set the src
  • 🛠 Flexible: Personalize the options to fit your needs
  • ⚡️ Smart: Async requests will be cached.
  • 🚀 SSR: Render a loader until the DOM is available
  • 🟦 Typescript: Nicely typed

Usage

npm i react-inlinesvg

And import it into your code:

import React from 'react';
import SVG from 'react-inlinesvg';

export default function App() {
  return (
    <main>
      <SVG
        src="https://cdn.svgporn.com/logos/react.svg"
        width={128}
        height="auto"
        title="React"
      />
    </main>
  );
}

Props

src {string} - required.
The SVG file you want to load. It can be a require, URL, or a string (base64 or URL encoded). If you are using create-react-app and your file resides in the public directory, you can use the path directly without require.

baseURL {string}
An URL to prefix each ID in case you use the <base> tag and uniquifyIDs.

children {ReactNode}
The fallback content in case of a fetch error or unsupported browser.

<SVG src="...">
	<img src="..." alt="fallback" />
</SVG>

cacheRequests {boolean} ▶︎ true
Cache remote SVGs.
Starting in version 4.x, you can also cache the files permanently, read more below.

description {string}
A description for your SVG. It will override an existing <desc> tag.

fetchOptions {RequestInit}
Custom options for the request.

innerRef {React.Ref}
Set a ref in SVGElement.

The SVG is processed and parsed so the ref won't be set on the initial render. You can use the onLoad callback to get and use the ref instead.

loader {node}
A component to be shown while the SVG is loading.

onError {function}
A callback to be invoked if loading the SVG fails.
This will receive a single argument with:

  • a FetchError with:
{
  message: string;
  type: string;
  errno: string;
  code: string;
}
  • or an InlineSVGError, which has the following properties:
{
  name: 'InlineSVGError';
  data: object; // optional
  message: string;
}

onLoad {function}.
A callback to be invoked upon successful load.
This will receive 2 arguments: the src prop and an isCached boolean

preProcessor {function} ▶︎ string
A function to process the contents of the SVG text before parsing.

title {string | null}
A title for your SVG. It will override an existing <title> tag.
If null is passed, the <title> tag will be removed.

uniqueHash {string} ▶︎ a random 8 characters string [A-Za-z0-9]
A string to use with uniquifyIDs.

uniquifyIDs {boolean} ▶︎ false
Create unique IDs for each icon.

Any additional props will be passed down to the SVG element.

Example

<SVG
  baseURL="/home"
  cacheRequests={true}
  description="The React logo"
  loader={<span>Loading...</span>}
  onError={(error) => console.log(error.message)}
  onLoad={(src, isCached) => console.log(src, isCached)}
  preProcessor={(code) => code.replace(/fill=".*?"/g, 'fill="currentColor"')}
  src="https://cdn.svgporn.com/logos/react.svg"
  title="React"
  uniqueHash="a1f8d1"
  uniquifyIDs={true}
/>

Caching

You can use the browser's cache to store the SVGs permanently.
To set it up, wrap your app with the cache provider:

import { createRoot } from 'react-dom/client';
import CacheProvider from 'react-inlinesvg/provider';
import App from './App';

createRoot(document.getElementById('root')!).render(
  <CacheProvider>
    <App />
  </CacheProvider>
);

Be aware of the limitations of the Cache API.

Browser Support

Any browsers that support inlining SVGs and fetch will work.

If you need to support legacy browsers, include a polyfill for fetch and Number.isNaN in your app.

CORS

If you are loading remote SVGs, you must ensure it has CORS support.

Why do you need this package?

One of the reasons SVGs are awesome is that you can style them with CSS. Unfortunately, this is not useful in practice because the style element has to be in the same document. This leaves you with three bad options:

  1. Embed the CSS in the SVG document
    • Can't use your CSS preprocessors (LESS, SASS)
    • Can't target parent elements (button hover, etc.)
    • Makes maintenance difficult
  2. Link to a CSS file in your SVG document
    • Sharing styles with your HTML means duplicating paths across your project, making maintenance a pain
    • Not sharing styles with your HTML means extra HTTP requests (and likely duplicating paths between different SVGs)
    • Still can't target parent elements
    • Your SVG becomes coupled to your external stylesheet, complicating reuse.
  3. Embed the SVG in your HTML
    • Bloats your HTML
    • SVGs can't be cached by browsers between pages.
    • A maintenance nightmare

But there's an alternative that sidesteps these issues: load the SVG with a GET request and then embed it in the document. This is what this component does.

Note

The SVG <use> element can be used to achieve something similar to this component. See this article for more information and this table for browser support and caveats.

Credits

Thanks to @matthewwithanm for creating this component and so kindly transferring it to me. I'll definitely keep up the good work! ❤️