Skip to content

Commit

Permalink
feat(jsx/dom): implement light weight memo function for DOM renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed Oct 27, 2024
1 parent 38a3c7e commit 3b45b57
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/jsx/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* This module provides APIs for `hono/jsx/dom`.
*/

import { isValidElement, memo, reactAPICompatVersion } from '../base'
import type { Child, DOMAttributes, JSX, JSXNode, Props } from '../base'
import { isValidElement, reactAPICompatVersion, shallowEqual } from '../base'
import type { Child, DOMAttributes, JSX, JSXNode, Props, FC, MemorableFC } from '../base'
import { Children } from '../children'
import { DOM_MEMO } from '../constants'
import { useContext } from '../context'
import {
createRef,
Expand Down Expand Up @@ -72,6 +73,15 @@ const cloneElement = <T extends JSXNode | JSX.Element>(
) as T
}

const memo = <T>(
component: FC<T>,
propsAreEqual: (prevProps: Readonly<T>, nextProps: Readonly<T>) => boolean = shallowEqual
): FC<T> => {
const wrapper = ((props: T) => component(props)) as MemorableFC<T>
wrapper[DOM_MEMO] = propsAreEqual
return wrapper as FC<T>
}

export {
reactAPICompatVersion as version,
createElement as jsx,
Expand Down

0 comments on commit 3b45b57

Please sign in to comment.