#Using svgs as react components

2 messages · Page 1 of 1 (latest)

unreal heathBOT
#

Hi @agile jolt. 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 support importing .astro components inside a UI framework component like .jsx or .svelte source (https://docs.astro.build/en/guides/framework-components/#can-i-use-astro-components-inside-my-framework-components). However, you can use the Astro <slot /> pattern to pass static content generated by Astro components as children to your framework components inside an .astro component.

When it comes to using SVGs as React components, you can certainly do that. You can import your SVGs directly into your React components and use them as you would any other component. Here's an example:

import stars from "../assets/stars.png";
export default function ReactImage() {  
  return (    
    <img src={stars.src} alt="A starry night sky." />  
  )
}

source (https://docs.astro.build/en/guides/images/#images-in-ui-framework-components)

The downside to this approach is that Astro will not optimize these images. However, you can install and use NPM packages for more flexibility.

If you want to use these SVGs inside of Astro components, you can pass the static content generated by <Image /> to a framework component inside a .astro file as children or using a named <slot/>:

---import ReactComponent from './ReactComponent.jsx';
import { Image } from 'astro:assets';
import stars from '~/stars/docline.png';---

<ReactComponent>  
  <Image src={stars} alt="A starry night sky." />
</ReactComponent>

source (https://docs.astro.build/en/guides/images/#images-in-ui-framework-components)

In conclusion, using SVGs as React components should not pose any significant downsides in an SSR environment, especially if you're dealing with a small number of icons.

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