react-svg is a React component designed to fetch SVG files from a given URL and inject their raw markup directly into the DOM, rather than rendering them as `<img>` tags. This approach, powered by the underlying `@tanem/svg-injector` library, enables full manipulation of the SVG's internal elements via CSS and JavaScript, unlocking possibilities like dynamic styling, animation, and interactivity that are not possible with traditional `<img>` elements. It also features client-side caching of fetched SVGs to reduce redundant network requests for repeated assets. The package is actively maintained, with its current stable version being 17.2.4, and exhibits a healthy release cadence, often seeing updates within a three-month window. It ships with TypeScript type definitions, enhancing development in TypeScript environments.
npm install react-svgVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic usage of `ReactSVG` to load an external SVG, including lifecycle callbacks, error handling, and a loading fallback. It highlights DOM manipulation before injection.
Provide a valid URL string to the `src` prop. If using raw SVG markup, parse it with `DOMParser` and append it, or use `dangerouslySetInnerHTML` after careful sanitization.
Configure the server hosting the SVG to include appropriate CORS headers, specifically `Access-Control-Allow-Origin`, to permit requests from your application's origin. For development, a proxy might temporarily circumvent this.
Always keep `evalScripts` set to its default value of 'never' unless you fully trust the source of all SVGs and understand the security implications. Thoroughly sanitize any user-uploaded or untrusted SVG content on the server-side to strip out malicious scripts before they reach the client.
Minimize frequent changes to the `src` prop if visual smoothness is critical. Consider preloading SVGs or using a CSS transition for the wrapper element to smooth out the transition. For very small, static SVGs, directly inlining them as JSX components or using an `<img>` tag might be preferable if re-injection flashes are problematic.
Populate the `title` prop with a concise, human-readable title and the `desc` prop with a more detailed description for complex SVGs. This improves usability for assistive technologies.
Verify the `src` URL is correct and the SVG file exists at that location. Check network requests in browser developer tools for the exact HTTP status code.
Configure the server hosting the SVG to send appropriate CORS headers that allow requests from your application's origin. For local development, a Webpack proxy or a browser extension might bypass this, but a server-side fix is essential for production.
The `src` prop requires a URL string. If you have SVG markup as a string, you must store it as a file and provide its URL, or manually parse and inject it into the DOM outside of `ReactSVG`.
Inspect the injected SVG in the browser's developer tools. Check its dimensions, applied CSS, and the integrity of its internal structure. Ensure `viewBox` is set correctly in the SVG. If using a bundler like Webpack with SVGR, ensure it's configured to output a compatible format or handle namespace tags correctly.