Skip to content
Myr

[BREAKING] refactor(jsx): `h` to `jsx`, remove `h` (#318)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Yusuke Wada · committed by GitHub · parent 21aabc4 · Browse files · 4 files+48 added−28 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+48 added−28 removed

  • tests:+3 added−4 removed
  • other:+45 added−24 removed

4 files+48 added−28 removed

modified:src/middleware/jsx/index.test.tsx+3 added-4 removed
modified:src/middleware/jsx/index.ts+3 added-15 removed
1import type { Context } from '../../context'2import type { Next } from '../../hono'1import { escape } from '../../utils/html'2import type { HtmlEscapedString } from '../../utils/html'34declare global {5 // eslint-disable-next-line @typescript-eslint/no-namespace8 namespace h.JSX {6 namespace jsx.JSX {7 interface IntrinsicElements {8 [tagName: string]: Record<string, any>9 }10 }11}1215export const jsx = () => {16 return async (c: Context, next: Next) => {17 c.render = (content: string) => {18 const output = `<!doctype html>${content.toString()}`19 return c.html(output)20 }21 await next()22 }23}2425export const h = (13export const jsx = (14 tag: string | Function,15 props: Record<string, any>,16 ...children: (string | HtmlEscapedString)[]17): HtmlEscapedString => {
85 unmodified lines
103 }) as FC<T>104}105106export const Fragment = (props: { key?: string; children?: any }): HtmlEscapedString => {119 return h('', {}, ...(props.children || []))107 return jsx('', {}, ...(props.children || []))108}
modified:src/middleware/jsx/README.md+41 added-8 removed
1# JSX Middleware23JSX Middleware enable rendering HTML with JSX syntax.4It's just for Sever-Side-Rendering. No virtual DOM.5This middleware supports only for TypeScript.5This middleware is only for writing with TypeScript.67## Settings89tsconfig.json:
1 unmodified line
11```json12{13 "compilerOptions": {14 "jsx": "react",15 "jsxFactory": "h"15 "jsxFactory": "jsx",16 "jsxFragmentFactory": "Fragment"17 }18}19```20
2 unmodified lines
23index.tsx:2425```tsx26import { Hono } from 'hono'26import { h, jsx } from 'hono/jsx'27import { jsx } from 'hono/jsx'2829const app = new Hono()3030app.use('*', jsx())3131const Layout = (props: { children?: string }) => {32 return (33 <html>34 <body>{props.children}</body>
15 unmodified lines
50}5152app.get('/', (c) => {53 const messages = ['Good Morning', 'Good Evening', 'Good Night']55 return c.render(<Top messages={messages} />)54 return c.htm(<Top messages={messages} />)55})5657app.fire()58```5960## dangerouslySetInnerHTML6162`dangerouslySetInnerHTML` allows you to set HTML directly.6364```tsx65app.get('/foo', (c) => {65 const html = { __html: 'JSX &middot; SSR' }66 return c.render(<div dangerouslySetInnerHTML={html} />)66 const inner = { __html: 'JSX &middot; SSR' }67 const Div = <div dangerouslySetInnerHTML={inner} />68})69```7071## memo7273You can memorize calculated strings of the component with `memo`.7475```tsx76import { jsx, memo } from 'hono/jsx'7778const Header = memo(() => <header>Welcome to Hono</header>)79const Footer = memo(() => <footer>Powered by Hono</footer>)80const Layout = (81 <div>82 <Header />83 <p>Hono is cool!</p>84 <Footer />85 </div>86)87```8889## Fragment9091```tsx92import { jsx, Fragment } from 'hono/jsx'9394const List = () => (95 <Fragment>96 <p>first child</p>97 <p>second child</p>98 <p>third child</p>99 </Fragment>100)101```102103## Tips for Cloudflare Workers104105It's useful to use Miniflare's`live-reload` option for developing.106
16 unmodified lines
modified:tsconfig.json+1 added-1 removed
17 unmodified lines
18 "node",19 "@cloudflare/workers-types"20 ],21 "jsx": "react",22 "jsxFactory": "h",22 "jsxFactory": "jsx",23 "jsxFragmentFactory": "Fragment",24 },25 "include": [26 "src/**/*.ts",
4 unmodified lines