react-inlinesvg vs react-svg vs react-svg-inline vs react-svg-loader vs svg-inline-react
React SVG Libraries
react-inlinesvgreact-svgreact-svg-inlinereact-svg-loadersvg-inline-reactSimilar 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-inlinesvg01,311189 kB09 days agoMIT
react-svg0881313 kB3a month agoMIT
react-svg-inline0222-38 years agoMIT
react-svg-loader0640-487 years agoMIT
svg-inline-react0132-165 years agoMIT

Feature Comparison: react-inlinesvg vs react-svg vs react-svg-inline vs react-svg-loader vs svg-inline-react

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

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

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

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

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

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

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

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

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

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

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

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

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-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>
    );
    
  • 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>
    );
    

How to Choose: react-inlinesvg vs react-svg vs react-svg-inline vs react-svg-loader vs svg-inline-react

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

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

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: Safe for server-side rendering

Usage

npm i react-inlinesvg

And import it into your code:

import SVG from 'react-inlinesvg';

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

Props

src {string} - required. The SVG to load. It accepts:

  • A URL or path to an SVG file (absolute or relative, including from a bundler import)
  • A data URI (base64 or URL-encoded)
  • A raw SVG string

baseURL {string} A URL to prepend to url() references inside the SVG when using uniquifyIDs. Required if your page uses an HTML <base> tag.

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

<SVG src="...">
	<img src="https://raw.githubusercontent.com/gilbarbara/react-inlinesvg/HEAD/..." alt="fallback" />
</SVG>

cacheRequests {boolean} ▶︎ true Cache remote SVGs in memory. When used with the CacheProvider, requests are also persisted in the browser cache.

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

fetchOptions {RequestInit}
Custom options for the request.

innerRef {React.Ref<SVGElement | null>} Set a ref on the SVG element.

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 Error for issues like missing src, unsupported browser, or invalid SVG content.

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

preProcessor {function} A function to pre-process the SVG string before parsing. Receives the SVG string and must return a string.

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.svglogos.dev/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>
);

The CacheProvider accepts an optional name prop to customize the cache storage name.

Be aware of the limitations of the Cache API.

Browser Support

Any browser that supports inlining SVGs and fetch will work.

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

CORS

If you are loading remote SVGs, you must ensure they have CORS headers.

Why do you need this package?

One reason 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! ❤️