#I want to integrate svgr plugins that allow importing svgs as React components into a React island.

3 messages · Page 1 of 1 (latest)

broken yoke
#

What is the preferred method for importing svgs as react components in Astro?

flat flameBOT
#

Hi @broken yoke. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Astro does not directly support importing SVGs as React components. However, there are several workarounds you can consider:

  1. Use an Astro component to wrap your SVG and then use it as a regular component. This method is straightforward and doesn't require any additional libraries.

  2. Use a library like @inlang/paraglide-js-adapter-astro or @jasikpark/astro-svg-loader that allows you to import SVGs as Astro components. These libraries use Vite's support for raw imports to do the trick.

Here is an example of how you might use @jasikpark/astro-svg-loader:

import MySvg from 'my-svg.svg';

Then, in your component, you can use MySvg as a regular component.

Please note that these methods might not work perfectly with React components in Astro. You might need to experiment a bit to find the best solution for your specific use case.

Sources:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: