Skill v1.0.0
currentAutomated scan100/100name: astro-react-islands description: "Trigger: astro react, react in astro, astro islands, client:load, client:only, astro + react. Enforce static-first islands: atomic React client islands, no monolithic wrappers, state via project-detected stores." license: MIT metadata: author: "strocs" version: "1.0"
Astro React Islands
Activation Contract
Load this skill when:
- Creating or modifying React components in an Astro project
- Adding
client:*directives to any framework component - User asks about Astro + React integration, interactivity, or hydration
- Reviewing code that mixes
.astroand.jsx/.tsxfiles
Hard Rules
- Astro owns the static shell. Layout, wrappers, text, images, and non-interactive markup stay in
.astrocomponents. - React handles ONLY interactivity. Every
client:*directive requires a justified interactive need (state changes, event handlers, DOM manipulation, real-time updates). - No monolithic React wrappers. Never wrap an entire feature tree in a single
client:loadReact component. This defeats islands architecture and ships unnecessary JavaScript. - Islands are atomic. Each React island has ONE interactive responsibility. Split compound features into independent islands.
- Detect, don't dictate state stores. Check
package.jsonfor existing state management (zustand,nanostores,jotai,valtio,@reduxjs/toolkit,mobx,recoil,xstate). Use what the project already has. If none exists, propose Nanostores (Astro's official recommendation, <1KB, framework-agnostic). - Astro `<script>` beats React for simple interactivity. A click handler, DOM toggle, or event listener that does not need hooks, React state, or cross-component communication belongs in an Astro
<script>tag — not a React island.
Decision Gates
Before writing ANY React component, apply these three gates in order:
| Gate | Question | If YES | |
|---|---|---|---|
| 1. Static extraction | Can this render as static .astro markup? | Keep it in .astro. Do NOT add client:*. | |
| 2. Script sufficiency | Can this be a plain <script> in .astro? (e.g., DOM toggle, event listener, no hooks needed) | Use <script> in .astro. Do NOT create a React component. | |
| 3. Atomic split | Does this React component do more than ONE interactive thing? | Split into multiple independent islands. Use a state store for cross-island communication if needed. |
Only after ALL THREE gates pass with "NO" — create a focused React island with the most appropriate client:* directive.
Execution Steps
- Analyze the feature. Identify what is static vs. what is interactive. List ALL interactive behaviors.
- Exhaust static extraction. For each behavior, ask: can
.astrohandle this? Can a<script>tag handle this? - Design atomic islands. For remaining interactive behaviors, design one React component per behavior. Each must have a single, clear responsibility.
- Detect state store. Grep
package.jsonforzustand\|nanostores\|jotai\|valtio\|@reduxjs/toolkit\|mobx\|recoil\|xstate. Use the detected store. If none, propose Nanostores +@nanostores/react. - Choose hydration strategy:
client:load— critical above-fold interactivity, immediate needclient:visible— below-fold, defer until component enters viewportclient:idle— non-critical, hydrate when browser is idleclient:only— skip SSR entirely (rare; only when component breaks during server render)client:media— only on matching media query
- Wire cross-island state. If two atomic islands need shared state, connect them through the detected store. Do NOT create a parent React wrapper to pass props or React context — context does not cross island boundaries.
- Verify. Confirm no static markup leaked into React components. Confirm each
client:*directive is justified.
Anti-Pattern — Monolithic Wrapper
// ❌ BAD: Astro is reduced to a shell. ReactFeature owns everything.---import ReactFeature from './ReactFeature';---<ReactFeature client:load />
Where ReactFeature contains layout divs, static text, static images, interactive toggles, forms, and modals — all in one React tree shipped as client JavaScript.
// ✅ GOOD: Astro owns the layout. React owns only interactive atoms.---import Toggle from './Toggle';import Modal from './Modal';---<section><h2>Feature Title</h2><p>Static description text lives in .astro — zero JS shipped.</p><Toggle client:idle /><Modal client:visible /></section>
State Store Detection
Run before proposing any state management:
grep -oE "zustand|nanostores|jotai|valtio|@reduxjs/toolkit|mobx|recoil|xstate" package.json | head -1
- Match found — use that store. Wire islands through it. Never force a migration.
- No match — propose
nanostores+@nanostores/react.
Script Over React
When interactivity is simple and self-contained, use <script> in .astro:
---// ✅ Simple toggle — no React needed---<button id="menu-toggle">Menu</button><nav id="menu" hidden>...</nav><script>document.getElementById('menu-toggle').addEventListener('click', () => {document.getElementById('menu').hidden = !document.getElementById('menu').hidden;});</script>
Use React only when the behavior needs: React hooks, React state management, integration with a state store, or composition with other React islands.
Output Contract
After designing or implementing React components in Astro, report:
- Each React island created, its
client:*directive, and its single responsibility. - Justification for why each island could NOT be static
.astroor a<script>tag. - The detected state store (or proposed Nanostores if none exists).
- Confirmation that no static markup leaked into React components.