53 lines
1.9 KiB
Markdown
53 lines
1.9 KiB
Markdown
---
|
|
description: |
|
|
Islands enable client side interactivity in Fresh. They are hydrated on the client in addition to being rendered on the server.
|
|
---
|
|
|
|
Islands enable client side interactivity in Fresh. Islands are isolated Preact
|
|
components that are rendered on the client. This is different from all other
|
|
components in Fresh, as they are usually just rendered on the server.
|
|
|
|
Islands are defined by creating a file in the `islands/` folder in a Fresh
|
|
project. The name of this file must be a PascalCase or kebab-case name of the
|
|
island. The file must have a default export that is a regular Preact component.
|
|
|
|
```tsx
|
|
// islands/MyIsland.tsx
|
|
|
|
import type { Signal } from "@preact/signals";
|
|
|
|
export default function MyIsland() {
|
|
const count = useSignal(0);
|
|
|
|
return (
|
|
<div>
|
|
Counter is at {count}. <button onClick={() => count.value += 1}>+</button>
|
|
</div>
|
|
);
|
|
}
|
|
```
|
|
|
|
An island can be used in a page like a regular Preact component. Fresh will take
|
|
care of automatically re-hydrating the island on the client.
|
|
|
|
Passing props to islands is supported, but only if the props are serializable.
|
|
Fresh can serialize the following types of values:
|
|
|
|
- Primitive types `string`, `boolean`, and `null`
|
|
- Most `number`s (`Infinity`, `-Infinity`, and `NaN` are silently converted to
|
|
`null`, and `bigint`s are not supported)
|
|
- Plain objects with string keys and serializable values
|
|
- Arrays containing serializable values
|
|
- Uint8Array
|
|
- Preact Signals (if the inner value is serializable)
|
|
|
|
Circular references are supported. If an object or signal is referenced multiple
|
|
times, it is only serialized once and the references are restored upon
|
|
deserialization.
|
|
|
|
Passing complex objects like `Date`, custom classes, or functions is not
|
|
supported. This means that it is not possible to pass `children` to an island,
|
|
as `children` are VNodes, which are not serializable.
|
|
|
|
It is also not supported to nest islands within other islands.
|