[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
Provenance
- Commit
d433211- Tree
50fa15f- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
21aabc4
4 files changed+48 added−28 removed
modified:src/middleware/jsx/index.test.tsx+3 added-4 removed
122345667810910111213151415161718165 unmodified linesExpand allimport { Hono } from '../../hono'import { h, jsx, memo, Fragment } from '.'import { jsx, memo, Fragment } from '.'
describe('JSX middleware', () => { const app = new Hono() app.use('*', jsx())
it('Should render HTML strings', async () => { app.get('/', (c) => { return c.render(<h1>Hello</h1>) return c.html(<h1>Hello</h1>) }) const res = await app.request('http://localhost/') expect(res.status).toBe(200) expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8') expect(await res.text()).toBe('<!doctype html><h1>Hello</h1>') expect(await res.text()).toBe('<h1>Hello</h1>') })})
describe('render to string', () => {165 unmodified linesExpand allmodified:src/middleware/jsx/index.ts+3 added-15 removed
1-import type { Context } from '../../context'2-import type { Next } from '../../hono'1 import { escape } from '../../utils/html'2 import type { HtmlEscapedString } from '../../utils/html'3 4 declare 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 }12 15-export 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-}24-25-export const h = (13+export const jsx = (14 tag: string | Function,15 props: Record<string, any>,16 ...children: (string | HtmlEscapedString)[]17 ): HtmlEscapedString => {85 unmodified lines103 }) as FC<T>104 }105 106 export 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 Middleware2 3 JSX Middleware enable rendering HTML with JSX syntax.4 It's just for Sever-Side-Rendering. No virtual DOM.5-This middleware supports only for TypeScript.5+This middleware is only for writing with TypeScript.6 7 ## Settings8 9 tsconfig.json:1 unmodified line11 ```json12 {13 "compilerOptions": {14 "jsx": "react",15- "jsxFactory": "h"15+ "jsxFactory": "jsx",16+ "jsxFragmentFactory": "Fragment"17 }18 }19 ```20 2 unmodified lines23 index.tsx:24 25 ```tsx26 import { Hono } from 'hono'26-import { h, jsx } from 'hono/jsx'27+import { jsx } from 'hono/jsx'28 29 const app = new Hono()30 30-app.use('*', jsx())31-31 const Layout = (props: { children?: string }) => {32 return (33 <html>34 <body>{props.children}</body>15 unmodified lines50 }51 52 app.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 })56 57 app.fire()58 ```59 60 ## dangerouslySetInnerHTML61 62+`dangerouslySetInnerHTML` allows you to set HTML directly.63+64 ```tsx65 app.get('/foo', (c) => {65- const html = { __html: 'JSX · SSR' }66- return c.render(<div dangerouslySetInnerHTML={html} />)66+ const inner = { __html: 'JSX · SSR' }67+ const Div = <div dangerouslySetInnerHTML={inner} />68 })69 ```70 71+## memo72+73+You can memorize calculated strings of the component with `memo`.74+75+```tsx76+import { jsx, memo } from 'hono/jsx'77+78+const Header = memo(() => <header>Welcome to Hono</header>)79+const Footer = memo(() => <footer>Powered by Hono</footer>)80+const Layout = (81+ <div>82+ <Header />83+ <p>Hono is cool!</p>84+ <Footer />85+ </div>86+)87+```88+89+## Fragment90+91+```tsx92+import { jsx, Fragment } from 'hono/jsx'93+94+const List = () => (95+ <Fragment>96+ <p>first child</p>97+ <p>second child</p>98+ <p>third child</p>99+ </Fragment>100+)101+```102+103 ## Tips for Cloudflare Workers104 105 It's useful to use Miniflare's`live-reload` option for developing.106 16 unmodified linesmodified:tsconfig.json+1 added-1 removed
17 unmodified lines18 "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