ข้ามไปยังเนื้อหา

บทนำ: Astro สำหรับนักพัฒนา React

คุณสร้าง UI ด้วย React ได้อยู่แล้ว เข้าใจ component, hook, state, และ JSX ดีอยู่แล้ว Astro ไม่ได้มาแทนที่ความรู้เหล่านั้น — แต่เป็นวิธีส่งมอบที่แตกต่างออกไป ซึ่งนำ mental model ของ React มาใช้ในจุดที่ควรใช้ และถอยออกไปในจุดที่ไม่จำเป็น

การเปลี่ยนแปลงที่ใหญ่ที่สุด: React ส่ง JavaScript runtime ไปยัง browser และ render UI ที่นั่น ส่วน Astro render UI ที่ server (หรือขณะ build) แล้วส่ง HTML ออกมา JavaScript จะถึง browser เฉพาะส่วนที่ต้องการ interactivity จริงๆ เท่านั้น

สำหรับเว็บไซต์ที่เน้นเนื้อหา — docs, หน้า marketing, blog, landing page — ความแตกต่างนี้มีนัยสำคัญมาก: Time to First Contentful Paint ที่เร็วขึ้น, bundle ที่เล็กลง, และ Core Web Vitals ที่ดีขึ้นโดยไม่ต้องปรับแต่งเอง

Next.js เป็น full-stack React framework ที่ยอดเยี่ยม Astro เหมาะสมเมื่อ:

  • เนื้อหาของคุณมีน้ำหนักมากกว่า interactivity หน้า static, blog, docs site, และ marketing site แทบไม่ส่ง JavaScript เลยกับ Astro ตามค่าเริ่มต้น
  • คุณต้องการผสมหลาย framework Astro ให้คุณวาง React, Svelte, Vue, หรือ Preact component ไว้คู่กันได้ — หรือจะไม่ใช้เลยก็ได้
  • คุณสนใจเรื่องขนาด bundle islands architecture ของ Astro ทำให้แต่ละ interactive widget ถูก hydrate แยกกัน ส่วนที่เหลือของหน้าเป็น HTML ล้วนๆ
  • คุณต้องการ routing แบบ file-based โดยไม่มี client-side router ทุก .astro page ใน src/pages/ กลายเป็น URL — ไม่มี Link component, ไม่มี router config, ไม่มี hydration overhead
React / Next.jsเทียบเท่าใน Astro
ไฟล์ component .jsx / .tsxไฟล์ component .astro
Function bodyFrontmatter (บล็อก ---)
return (<JSX />)HTML template ด้านล่าง ---
argument propsAstro.props
prop children<slot />
useEffect / useStateFramework island ที่มี directive client:*
getServerSidePropsFrontmatter (fetch ขณะ build/request)
ไฟล์ Pages Routersrc/pages/*.astro
  • syntax {expr} แบบ JSX ใน template
  • การประกอบ component (<Header />, <Footer />)
  • TypeScript ทุกที่ (interface Props)
  • React component เดิมของคุณ — นำมาใส่เป็น island ได้เลย
  • tooling npm/pnpm, มี Vite อยู่เบื้องหลัง
โมดูลสิ่งที่คุณจะได้เรียน
Intro (โมดูลนี้)ทำไมต้อง Astro, islands architecture, การตั้งค่า, โครงสร้างโปรเจกต์, หน้าแรก
Componentsanatomy ของ .astro component, props, slots, expressions
Layoutsshared layout, nested layout, pattern ของ <slot />
Routingrouting แบบ file-based, dynamic route, หน้า 404
Data Fetchingfetch ใน frontmatter, getStaticPaths, content collection
Islandsการใช้ React/Svelte/Vue component กับ directive client:*

นี่คือการเปรียบเทียบที่กระชับที่สุด: React component ที่ render ส่วน hero กับ Astro page ที่เทียบเท่ากัน

React
// src/app/page.tsx (Next.js App Router)
export default function HeroPage() {
return (
<html lang="en">
<head><title>My Site</title></head>
<body>
<h1>Hello from React</h1>
<p>This page ships a full React runtime.</p>
</body>
</html>
);
}
Astro
---
// src/pages/index.astro
const title = "My Site";
---
<html lang="en">
<head><title>{title}</title></head>
<body>
<h1>Hello from Astro</h1>
<p>This page ships zero JavaScript.</p>
</body>
</html>

หน้า Astro ด้านบนส่ง JavaScript ศูนย์ชิ้นไปยัง browser — ตัวแปร title ถูกประเมินครั้งเดียวขณะ build และฝังลงใน HTML เลย ไม่มี React runtime, ไม่มี hydration, ไม่มี virtual DOM

Astro
---
const framework = "Astro";
const audience = "React developer";
const year = new Date().getFullYear();
const features = ["Zero JS by default", "Islands architecture", "Use React components as islands", "File-based routing"];
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Welcome to {framework}</title>
    <style>
      body { font-family: sans-serif; max-width: 640px; margin: 2rem auto; padding: 0 1rem; }
      ul { line-height: 2; }
      li::marker { color: #6366f1; }
    </style>
  </head>
  <body>
    <h1>Welcome, {audience}!</h1>
    <p>This is an Astro page rendered in {year}. Check the browser DevTools — no JS bundle.</p>
    <h2>Why Astro?</h2>
    <ul>
      {features.map(f => <li>{f}</li>)}
    </ul>
  </body>
</html>
หน้า Astro ส่งอะไรไปยัง browser ตามค่าเริ่มต้น?
โค้ดภายใน frontmatter ของ Astro (บล็อก ---) รันเมื่อใด?
ฟีเจอร์ใดของ Astro ที่ให้คุณใช้ React component ภายใน Astro page ได้?
อะไรคือสิ่งที่เทียบเท่า `getServerSideProps` ของ Next.js ใน Astro?