hono/jsx/dom module provides client-side rendering capabilities with a React-compatible API. It allows you to build interactive user interfaces that run in the browser.
Setup
Configure yourtsconfig.json for client-side JSX:
tsconfig.json
Basic Rendering
Use therender() function to mount components to the DOM:
Using createRoot (React 18+ API)
For better control over rendering, use thecreateRoot API:
Hooks
Hono JSX DOM supports all major React hooks:useState
Manage component state:useEffect
Perform side effects:useRef
Access DOM elements directly:useCallback
Memoize callback functions:useMemo
Memoize expensive computations:useReducer
Manage complex state logic:useContext
Access context values:useLayoutEffect
Perform DOM measurements before paint:useId
Generate unique IDs for accessibility:Event Handling
Handle user interactions with event handlers:Hydration
Hydrate server-rendered HTML:In Hono’s implementation,
hydrateRoot is equivalent to createRoot().render(). The actual DOM is rendered from scratch.Refs and DOM Access
Callback Refs
Ref Objects
forwardRef
Forward refs to child components:Suspense and Error Boundaries
Suspense
Handle async operations:ErrorBoundary
Catch and handle errors:Transitions
useTransition
Mark updates as transitions:useDeferredValue
Defer rendering of non-critical updates:Portals
Render children outside the parent hierarchy:flushSync
Force synchronous updates:Advanced Hooks
useSyncExternalStore
Subscribe to external stores:use (React 19)
Unwrap promises in render:Full Example: Todo App
Best Practices
Keep components small and focused
Keep components small and focused
Break down complex components into smaller, reusable pieces.
Use TypeScript for type safety
Use TypeScript for type safety
Define proper types for props, state, and event handlers.
Optimize with useMemo and useCallback
Optimize with useMemo and useCallback
Memoize expensive computations and callbacks to prevent unnecessary re-renders.
Clean up effects properly
Clean up effects properly
Always return cleanup functions from
useEffect to prevent memory leaks.Use keys for lists
Use keys for lists
Always provide unique keys when rendering lists to help with reconciliation.
Next Steps
Server Rendering
Learn about server-side JSX rendering
Streaming
Stream HTML with Suspense patterns
JSX Overview
Back to JSX overview