Skip to content
Myr

fix(jsx): allow null, undefined, and boolean to be returned from function component (#3241)

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

* fix(jsx): allow `null`, `undefined`, and `boolean` to be returned from function component * fix(jsx): allow `null` to be returned from `FC` type * test: add test for empty fragment in `"jsx": "precompile"` Empty Fragment is converted to null in `"jsx": "precompile"`, so add a test for that pattern

authored by Taku Amano · committed by GitHub · parent e123546 · Browse files · 3 files+32 added−5 removed
This commit

State

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

Diff stats

3 files+32 added−5 removed

  • tests:+24 added−0 removed
  • other:+8 added−5 removed

3 files+32 added−5 removed

modified:runtime_tests/deno-jsx/jsx.test.tsx+6 added
modified:src/jsx/base.ts+8 added-5 removed
13 unmodified lines
1415// eslint-disable-next-line @typescript-eslint/no-explicit-any16export type Props = Record<string, any>17export type FC<P = Props> = {18 (props: P): HtmlEscapedString | Promise<HtmlEscapedString>18 (props: P): HtmlEscapedString | Promise<HtmlEscapedString> | null19 defaultProps?: Partial<P> | undefined20 displayName?: string | undefined21}22export type DOMAttributes = HonoJSX.HTMLAttributes
223 unmodified lines
246 ...this.props,247 children: children.length <= 1 ? children[0] : children,248 })249250 if (typeof res === 'boolean' || res == null) {251 // boolean or null or undefined252 return250 if (res instanceof Promise) {253 } else if (res instanceof Promise) {254 if (globalContexts.length === 0) {255 buffer.unshift('', res)256 } else {257 // save current contexts for resuming
115 unmodified lines
373export const memo = <T>(374 component: FC<T>,375 propsAreEqual: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean = shallowEqual376): FC<T> => {374 let computed: HtmlEscapedString | Promise<HtmlEscapedString> | undefined = undefined377 let computed: ReturnType<FC<T>> = null378 let prevProps: T | undefined = undefined376 return ((props: T & { children?: Child }): HtmlEscapedString | Promise<HtmlEscapedString> => {379 return ((props) => {380 if (prevProps && !propsAreEqual(prevProps, props)) {378 computed = undefined381 computed = null382 }383 prevProps = props384 return (computed ||= component(props))385 }) as FC<T>
34 unmodified lines
modified:src/jsx/index.test.tsx+18 added
381 unmodified lines
382 expect(Top.toString()).toBe(383 '<html><head><title>Home page</title></head><body><h1>Hono</h1><p>Hono is great</p></body></html>'384 )385 })386387 describe('Booleans, Null, and Undefined Are Ignored', () => {388 it.each([true, false, undefined, null])('%s', (item) => {389 const Component: FC = (() => {390 return item391 }) as FC392 const template = <Component />393 expect(template.toString()).toBe('')394 })395396 it('falsy value', () => {397 const Component: FC = (() => {398 return 0399 }) as unknown as FC400 const template = <Component />401 expect(template.toString()).toBe('0')402 })403 })404 })405406 describe('style attribute', () => {407 it('should convert the object to strings', () => {
611 unmodified lines