Island preact

설치

bun x astro add preact --yes

react와 비슷한 경량 라이브러리이다.
bun add @astrojs/preact@^5.1.2 preact@^10.29.1 가 추가됨.
astro.config.mjs 에 통합됨.
tsconfig.json 에 jsx 등록됨.

사용

예시

카운트버튼 컴포넌트

import { useState } from 'preact/hooks';
import '@styles/global.css';

export default function CountButton() {
    const [count, setCount] = useState(0);
    return (
        <button class='btn' onClick={() => setCount(count + 1)}>{count}</button>
    );
}

astro 에서 사용

---
import CountButton from '@/components/CountButton';
---
<CountButton client:load/>

Island Astro

소개

props 전달

---
const { title } = Astro.props;
---

props 타입 지정

interface Props {
    title: string;
}

slot

<div>
    <slot/>
</div>

예시

---
import FormattedDate from '@components/FormattedDate.astro';

interface Props{
    title: string;
    description: string;
    pubDate: Date;
    href: string;
}

const { title, description, pubDate, href } = Astro.props;
---

<style>
    @reference "@styles/global.css";
    .s_btn {
        @apply cursor-pointer ;
    }
    
</style>

<a href={href} class="s_btn text-base-content no-underline w-full shadow-xl bg-primary/10 rounded-2xl p-2">
    <p class="font-bold border-b border-secondary/50 text-center">{title}</p>
    <p>{description}</p>
    <div class="text-end text-base-content/50 text-xs"><FormattedDate date={pubDate} /></div>
</a>


<script>

</script>