26
loading...
This website collects cookies to deliver better user experience
npm install --save-dev @astrojs/renderer-preact preact
renderers
property should be an empty Array. Add the @astrojs/renderer-preact
package to the Array.renderers: ['@astrojs/renderer-preact']
// /src/components/Counter.tsx
import { useState } from 'preact/hooks'
export default function Counter() {
const [count, setCount] = useState(0)
return (
<div className='counter'>
<button onClick={() => setCount(count - 1)} disabled={count === 0}>
-
</button>
<h2>{count}</h2>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
)
}
---
// /src/pages/index.astro
import Counter from '../components/Counter.tsx'
---
<Counter client:load />
client:load
snippet you see above comes from Astro's Partial Hydration. If we leave this away, Astro would only render the components markup, without any interactivity, to ship less client-side JavaScript. You can learn more about Partial Hydration in the official Astro Docs