You can’t use Astro components inside React, Vue, Svelte, or other framework components. So Astro’s Image and Picture won’t work. Splendid Astro’s Image component won’t work either.
<script> // Cannot import any Astro stuff directly into frameworks import { Image } from 'astro:assets'</script>
<Image ... />Inserting images into framework components
There are a few ways to insert images into framework components.
- Import the image (without optimization)
- Pass the image via a
slot - Pass the image via a
prop
The second one is my personal favourite.
Import the image directly
You can import an image directly inside a framework component.
<script> import stars from '../assets/stars.png'</script>
<img src={stars.src} width={stars.width} height={stars.height} alt="A starry night sky."/>Two important notes here:
- This image will not be optimized
- You must include
widthandheightto stop layout shifts. Astro’s docs show this pattern but they left outwidthandheight.
The image is not optimized because it doesn’t pass through Astro’s image pipeline. Because of this, I don’t consider this to be a viable option. But, it’s written here because some people think it’s passable.
On to the real solutions next.
Passing images through a slot
The best option is to build the Image component in your Astro file and pass it into your framework component through a slot. You can usually secure the right placement with a named slot.
---import { Image } from '@splendidlabz/astro'import SvelteComponent from '@/components/YourComponent.svelte'---
<SvelteComponent> <Image slot="image" src="/src/assets/image.jpg" alt="..." /></SvelteComponent>Use this whenever you can.
Passing images through a prop
There’s a third option that’s rather cumbersome.
In this case, you can use Astro’s getImage utility to optimize the image, get the attributes, then pass it into your framework component.
---import { getImage } from 'astro:assets'import SvelteComponent from '@/components/Component.svelte'import hero from '../assets/hero.jpg'
const image = await getImage({ src: hero, widths: [400, 800, 1200], // ...other options})---
<SvelteComponent {image} alt="..." />Inside the framework component, you need to unpack the attributes and write the img tag yourself.
<script> const { image, alt } = $props() const { src, attributes, srcSet } = image</script>
<img {...attributes} {src} {alt} srcset={srcSet.attribute} />Now, Astro’s getImage contains the standard features built for Astro’s Image and Picture components. It doesn’t contain the improvements we made.
If you prefer this method, you can also use the img utility from Splendid Astro to include the improvements we’ve made.
---import { img } from '@splendidlabz/astro/utils'import SvelteComponent from '@/components/Component.svelte'
const image = await img('/src/assets/hero.jpg', { widths: [400, 800, 1200], // ...other options})---
<SvelteComponent {image} alt="..." />Code to copy
The best method — hands down — is to pass the image through a slot.
---import { Image } from '@splendidlabz/astro'import SvelteComponent from '@/components/YourComponent.svelte'---
<SvelteComponent> <Image slot="image" src="/src/assets/image.jpg" alt="..." /></SvelteComponent>Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson