#Optimized image in react
11 messages · Page 1 of 1 (latest)
Hi @sand jackal. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Currently, Astro's built-in <Image> component from astro:assets is designed for use in .astro files. In React (.jsx) components, you should use the standard JSX image syntax (<img />). Astro will not optimize these images, but you can install and use NPM packages for more flexibility.
<img src="/assets/stars.png" alt="A starry night sky." />
You can learn more about using images in Astro (https://docs.astro.build/en/guides/images/) in the Images Guide.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@sand jackal kapa.ai is still learning and improving, please let us how it did by reacting below
@drowsy fossil is there a way to optimize images directly in mdx and mdoc files with "astro:assets"?
Let me check the knowledge base for you. One moment! :books:
Yes, you can use the built-in <Image> component from astro:assets in MDX files to optimize images. First, import the Image component and your image, then use the component with the imported image as the src attribute.
Here's an example of how to use the <Image> component in an MDX file:
import { Image } from 'astro:assets';
import rocket from '../assets/rocket.png';
<Image src={rocket} alt="Spaceship approaching the moon." />
For more information, refer to the Using in MDX (https://docs.astro.build/en/guides/images#using-in-mdx) section in the Images Guide.
However, there is no mention of using astro:assets in Markdoc files in the provided extracts.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@sand jackal kapa.ai is still learning and improving, please let us how it did by reacting below
@drowsy fossil what are relative images in "astro:assets"?
Let me check the knowledge base for you. One moment! :books:
Relative images in astro:assets refer to images that are located within your project's src/ directory. You can import these images using a relative file path or an import alias (https://docs.astro.build/en/guides/aliases/). Once imported, you can use the <Image> component from astro:assets with the imported image as the src attribute.
Here's an example of how to use a relative image with the <Image> component in an .astro file:
---
import { Image } from 'astro:assets';
import myImage from "../assets/my_image.png"; // Image is 1600x900
---
<!-- `alt` is mandatory on the Image component -->
<Image src={myImage} alt="A description of my image." />