A React component that uses SVGInjector to dynamically add SVG to the DOM.
$ npm install react-svg --save
There are also UMD builds available in the dist
directory. If you use these, make sure you have already included React as a dependency.
import React from 'react';
import ReactDOM from 'react-dom';
import ReactSVG from '../src/index.js';
ReactDOM.render(
<ReactSVG
path={'atomic.svg'}
className={'example'}
callback={(svg) => console.log(svg)}
/>,
document.querySelector('.Root')
);
To run the above example:
$ npm start
Then open a browser at localhost:8080
.
Props
path
- Path to the SVG.className
- Optional Class name to be added to the SVG.evalScripts
- Optional Run any script blocks found in the SVG (always
,once
, ornever
). Defaults tonever
.fallbackPath
- Optional Path to the fallback PNG.callback
- Optional Function to call after the SVG is injected. Receives the newly injected SVG DOM element as a parameter. Defaults tonull
.
Example
<ReactSVG
path={'atomic.svg'}
className={'example'}
evalScript={'always'}
fallbackPath={'atomic.png'}
callback={(svg) => console.log(svg)}
/>
Refer to the SVGInjector configuration docs for more information.
$ npm test
The release script for this module uses npm-version under the hood, so you should pass a semver string or release type as an argument.
For example, to publish a patch
release:
$ npm run release -- patch
react-svg does not currently support being rendered in Node. This is because SVGInjector uses XMLHttpRequest, something that Node does not have locally. It would be nice to rewrite SVGInjector to use something environment agnostic, like superagent or anything along those lines. This would allow react-svg to be rendered on both client and server.
MIT