|
| 1 | +# Islas |
| 2 | + |
| 3 | +Hasta ahora hemos visto como generar contenido estático en nuestro sitio Astro, y también ejecutar JS en el navegador, pero hay veces que nos hace falta meter componentes ricos, y hacerlos a pelo en vanilla JS puede ser un infierno ¿Qué podemos hacer? Astro nos permite meter componentes de React, Vue, Svelte, Angular, etc. en nuestro sitio, para ello utilizan el |
| 4 | +concepto de "islas". |
| 5 | + |
| 6 | +Vamos a: |
| 7 | + |
| 8 | +- Añadir un framework de UI, en este caso PReact. |
| 9 | +- Usar Preact para crear un componente interactivo de saludos. |
| 10 | +- Aprender cuando NO usar islas. |
| 11 | + |
| 12 | +> ¿Por qué usar PReact? porque es una librería muy ligera (pesa 3KB) |
| 13 | +
|
| 14 | +# Manos a la obra |
| 15 | + |
| 16 | +Lo primero, añadimos preact al proyecto, para ello necesitamos utilizar el CLI de Astro. |
| 17 | + |
| 18 | +```bash |
| 19 | +npx astro add preact |
| 20 | +``` |
| 21 | + |
| 22 | +Le decimos que si a todo :). |
| 23 | + |
| 24 | +Vamos a añadir un componente tonto de preact: |
| 25 | + |
| 26 | +_./src/components/greeting.tsx_ |
| 27 | + |
| 28 | +```tsx |
| 29 | +import type { FunctionComponent, h } from "preact"; |
| 30 | +import { useState } from "preact/hooks"; |
| 31 | + |
| 32 | +interface GreetingProps { |
| 33 | + messages: string[]; |
| 34 | +} |
| 35 | + |
| 36 | +const Greeting: FunctionComponent<GreetingProps> = ({ messages }) => { |
| 37 | + const randomMessage = (): string => |
| 38 | + messages[Math.floor(Math.random() * messages.length)]; |
| 39 | + |
| 40 | + const [greeting, setGreeting] = useState<string>(messages[0]); |
| 41 | + |
| 42 | + return ( |
| 43 | + <div> |
| 44 | + <h3>{greeting}! Thank you for visiting!</h3> |
| 45 | + <button onClick={() => setGreeting(randomMessage())}>New Greeting</button> |
| 46 | + </div> |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +export default Greeting; |
| 51 | +``` |
| 52 | + |
| 53 | +Y ahora vamos a suarlo en nuestra página principal: |
| 54 | + |
| 55 | +_./src/pages/index.astro_ |
| 56 | + |
| 57 | +```diff |
| 58 | +--- |
| 59 | +import BaseLayout from "../layouts/base.astro"; |
| 60 | ++ import Greeting from '../components/greeting'; |
| 61 | +--- |
| 62 | + |
| 63 | +<BaseLayout pageTitle="Página principal"> |
| 64 | + <p>Contenido de la página</p> |
| 65 | ++ <Greeting messages={["Hi", "Hello", "Howdy", "Hey there"]} /> |
| 66 | +</BaseLayout> |
| 67 | +``` |
| 68 | + |
| 69 | +Si ejecutamos esto ahora, vemos que aparece el componente, pero si clicamos en el botón, no funciona, tenemos que indicarle que lo cargue en el navegador, eso lo hacemos con `client:load`: |
| 70 | + |
| 71 | +_./src/pages/index.astro_ |
| 72 | + |
| 73 | +```diff |
| 74 | +<BaseLayout pageTitle="Página principal"> |
| 75 | + <p>Contenido de la página</p> |
| 76 | + <Greeting |
| 77 | ++ client:load |
| 78 | + messages={["Hi", "Hello", "Howdy", "Hey there"]} |
| 79 | + /> |
| 80 | +</BaseLayout> |
| 81 | +``` |
| 82 | + |
| 83 | +Con esto ya tenemos el componente funcionando en cliente. |
0 commit comments