> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/honojs/hono/llms.txt
> Use this file to discover all available pages before exploring further.

# JSX Overview

> Introduction to JSX in Hono - a powerful templating solution for server-side and client-side rendering

Hono provides a comprehensive JSX implementation that works seamlessly for both server-side and client-side rendering. Unlike traditional JSX libraries, Hono's JSX is designed to be lightweight, fast, and works directly with Hono's request/response model.

## What is Hono JSX?

Hono JSX is a React-like JSX implementation that:

* **Works on the server** - Renders directly to HTML strings
* **Works on the client** - Provides DOM rendering capabilities
* **React-compatible** - Uses familiar React APIs and hooks
* **Type-safe** - Full TypeScript support with proper type inference
* **No build required** - Works with standard JSX transpilation

<Note>
  Hono JSX is compatible with React 19 APIs and provides hooks like `useState`, `useEffect`, `useContext`, and more.
</Note>

## TypeScript Configuration

To use JSX in your Hono project, configure your `tsconfig.json`:

### For Server-Side Rendering

```json tsconfig.json theme={null}
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "hono/jsx"
  }
}
```

### For Client-Side Rendering

```json tsconfig.json theme={null}
{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "hono/jsx/dom"
  }
}
```

## Basic Usage

Here's a simple example using JSX in a Hono application:

```tsx theme={null}
import { Hono } from 'hono'

const app = new Hono()

const Layout = ({ children }: { children: any }) => {
  return (
    <html>
      <body>
        <header>
          <h1>My App</h1>
        </header>
        <main>{children}</main>
      </body>
    </html>
  )
}

const Home = () => {
  return (
    <div>
      <h2>Welcome</h2>
      <p>This is rendered with Hono JSX!</p>
    </div>
  )
}

app.get('/', (c) => {
  return c.html(
    <Layout>
      <Home />
    </Layout>
  )
})

export default app
```

## JSX Components

Hono JSX supports functional components with props:

```tsx theme={null}
type GreetingProps = {
  name: string
  age?: number
}

const Greeting = ({ name, age }: GreetingProps) => {
  return (
    <div>
      <h2>Hello, {name}!</h2>
      {age && <p>You are {age} years old.</p>}
    </div>
  )
}

app.get('/greet/:name', (c) => {
  const name = c.req.param('name')
  return c.html(<Greeting name={name} age={25} />)
})
```

## Props and Children

Components receive props and children just like React:

```tsx theme={null}
import type { FC, PropsWithChildren } from 'hono/jsx'

type CardProps = PropsWithChildren<{
  title: string
  variant?: 'primary' | 'secondary'
}>

const Card: FC<CardProps> = ({ title, variant = 'primary', children }) => {
  return (
    <div className={`card card-${variant}`}>
      <h3>{title}</h3>
      <div className="card-body">
        {children}
      </div>
    </div>
  )
}

// Usage
<Card title="My Card" variant="primary">
  <p>Card content goes here</p>
</Card>
```

## hono/jsx vs hono/jsx/dom

Hono provides two JSX implementations:

<CardGroup cols={2}>
  <Card title="hono/jsx" icon="server">
    **Server-side rendering**

    * Renders to HTML strings
    * Used in route handlers with `c.html()`
    * Optimized for server performance
    * Supports async components
  </Card>

  <Card title="hono/jsx/dom" icon="browser">
    **Client-side rendering**

    * Renders to actual DOM
    * Used for interactive UIs
    * React-compatible hooks
    * Supports hydration
  </Card>
</CardGroup>

## Attributes and HTML Properties

Hono JSX supports standard HTML attributes:

```tsx theme={null}
const Form = () => {
  return (
    <form method="POST" action="/submit">
      <input 
        type="text" 
        name="username" 
        placeholder="Enter username"
        required 
      />
      <input 
        type="password" 
        name="password" 
        minLength={8}
      />
      <button type="submit">Submit</button>
    </form>
  )
}
```

### Style Objects

Styles can be passed as objects:

```tsx theme={null}
const StyledDiv = () => {
  const style = {
    backgroundColor: 'blue',
    color: 'white',
    padding: '20px',
    borderRadius: '8px'
  }
  
  return <div style={style}>Styled content</div>
}
```

## Fragments

Use fragments to return multiple elements:

```tsx theme={null}
import { Fragment } from 'hono/jsx'

const List = () => {
  return (
    <>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
    </>
  )
}

// Or explicitly
const ListExplicit = () => {
  return (
    <Fragment>
      <li>Item 1</li>
      <li>Item 2</li>
    </Fragment>
  )
}
```

## Conditional Rendering

Render elements conditionally:

```tsx theme={null}
const UserStatus = ({ isLoggedIn, username }: { isLoggedIn: boolean; username?: string }) => {
  return (
    <div>
      {isLoggedIn ? (
        <p>Welcome back, {username}!</p>
      ) : (
        <p>Please log in</p>
      )}
    </div>
  )
}
```

## Lists and Keys

Render lists with keys:

```tsx theme={null}
type TodoItem = {
  id: number
  text: string
  completed: boolean
}

const TodoList = ({ items }: { items: TodoItem[] }) => {
  return (
    <ul>
      {items.map((item) => (
        <li key={item.id} className={item.completed ? 'completed' : ''}>
          {item.text}
        </li>
      ))}
    </ul>
  )
}
```

## dangerouslySetInnerHTML

Insert raw HTML (use with caution):

```tsx theme={null}
const RawHTML = ({ html }: { html: string }) => {
  return <div dangerouslySetInnerHTML={{ __html: html }} />
}
```

<Warning>
  Only use `dangerouslySetInnerHTML` with trusted content to prevent XSS attacks.
</Warning>

## API Compatibility

Hono JSX exports a `version` that indicates React API compatibility:

```tsx theme={null}
import { version } from 'hono/jsx'

console.log(version) // '19.0.0-hono-jsx'
```

Source: [src/jsx/base.ts:442](https://github.com/honojs/hono/blob/main/src/jsx/base.ts#L442)

## Next Steps

<CardGroup cols={2}>
  <Card title="Server Rendering" icon="server" href="/jsx/server-rendering">
    Learn about server-side JSX rendering with Hono
  </Card>

  <Card title="DOM Rendering" icon="browser" href="/jsx/dom-rendering">
    Build interactive UIs with client-side rendering
  </Card>

  <Card title="Streaming" icon="bolt" href="/jsx/streaming">
    Stream HTML with Suspense-like patterns
  </Card>

  <Card title="Helpers" icon="sparkles" href="/helpers/html-css">
    Use the HTML helper for advanced rendering
  </Card>
</CardGroup>
