#Dynamically import images from array
3 messages · Page 1 of 1 (latest)
Hi @loud sluice. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro allows you to import images into your .astro files for use with the built-in <Image /> component. However, the provided extracts do not contain specific information on dynamically importing images from an array.
Here's a general approach based on the information available:
- Import your images into your
.astrofile. - Store the imported images in an array.
- Use the array elements (images) as the
srcattribute in the<Image />component.
Here's an example of how you might do this:
---
import { Image } from 'astro:assets';
import image1 from '../../images/subfolder/image1.png';
import image2 from '../../images/subfolder/image2.png';
import image3 from '../../images/subfolder/image3.png';
const images = [image1, image2, image3];
---
{images.map((image, index) => (
<Image key={index} src={image} alt={`Image ${index + 1}`} />
))}