Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(readme): add context in islands and format #108

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ This is a `_renderer.tsx`, which will load the `/app/client.ts` entry file for t
```tsx
// app/routes/_renderer.tsx
import { jsxRenderer } from 'hono/jsx-renderer'
import { HasIslands } from "honox/server";
import { HasIslands } from 'honox/server'

export default jsxRenderer(({ children }) => {
return (
Expand Down Expand Up @@ -322,7 +322,7 @@ createClient()

### Interactions

Function components placed in `app/islands/*` are also sent to the client side. For example, you can write interactive component such as the following counter:
Function components placed in `app/islands/*` - Island components - are also sent to the client side. For example, you can write interactive component such as the following counter:

```tsx
// app/islands/counter.tsx
Expand Down Expand Up @@ -356,6 +356,18 @@ export default createRoute((c) => {
})
```

**Note**: You cannot access a Context object in Island components. Therefore, you should pass the value from components outside of Island.

```ts
import { useRequestContext } from 'hono/jsx-renderer'
import Counter from '../islands/counter.tsx'

export default function Component() {
const c = useRequestContext()
return <Counter init={parseInt(c.req.query('count') ?? '0', 10)} />
}
```

## BYOR - Bring Your Own Renderer

You can bring your own renderer using a UI library like React, Preact, Solid, or others.
Expand Down Expand Up @@ -457,8 +469,8 @@ Let's start with our route handler:
```tsx
// app/routes/nested/index.tsx
export default createRoute((c) => {
return c.render(<div>Content</div>, { title: 'Dashboard' });
});
return c.render(<div>Content</div>, { title: 'Dashboard' })
})
```

Now, let's take a look at our nested renderer:
Expand All @@ -467,11 +479,12 @@ Now, let's take a look at our nested renderer:
// app/routes/nested/_renderer.tsx
export default jsxRenderer(({ children, Layout, title }) => {
return (
<Layout title={title}> {/* Pass the title prop to the parent renderer */}
<Layout title={title}>
{/* Pass the title prop to the parent renderer */}
<main>{children}</main>
</Layout>
);
});
)
})
```

In this setup, all the props sent to the nested renderer's <Layout /> are consumed by the parent renderer:
Expand All @@ -480,16 +493,16 @@ In this setup, all the props sent to the nested renderer's <Layout /> are consum
// app/routes/_renderer.tsx
export default jsxRenderer(({ children, title }) => {
return (
<html lang="en">
<html lang='en'>
<head>
<title>{title}</title> {/* Use the title prop here */}
</head>
<body>
{children} {/* Insert the Layout's children here */}
</body>
</html>
);
});
)
})
```

### Using Middleware
Expand Down