Skip to content

.astro Components

In React you write components as JavaScript functions that return JSX. In Astro you write components as .astro files — a superset of HTML that has an optional JavaScript/TypeScript block at the top called the frontmatter.

The mental model shift: a React function component runs in the browser on every render. An Astro component runs once at build time (or on the server) and ships pure HTML — zero JavaScript by default. No virtual DOM, no re-renders, no hydration cost.

This module walks through every building block of a .astro component, anchored to what you already know from React.

LessonWhat you’ll learn
Frontmatter & TemplateThe --- block vs a React function body
PropsAstro.props vs React props
Slots<slot /> vs children
ExpressionsTemplate expressions vs JSX
Importing ComponentsComposition in .astro
Server-Only & ScriptsZero JS by default; adding browser JS

A React component and its .astro equivalent side by side:

React
// Greeting.jsx
export default function Greeting({ name }) {
return <h1>Hello, {name}!</h1>;
}
Astro
---
// Greeting.astro
const { name } = Astro.props;
---
<h1>Hello, {name}!</h1>

And here is the same idea as a runnable page:

Astro
---
const name = "React developer";
const year = new Date().getFullYear();
---
<html lang="en">
  <head><meta charset="utf-8" /><title>Hello</title></head>
  <body>
    <h1>Hello, {name}!</h1>
    <p>Built in {year}. No JavaScript shipped to the browser.</p>
  </body>
</html>
When does the code inside an Astro component's frontmatter (---) run?
How much JavaScript does a plain .astro component ship to the browser by default?
Which syntax does an .astro template use to embed expressions?