Skip to content

Commit

Permalink
fix(jsx/dom): fix memo for DOM renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma committed Oct 27, 2024
1 parent 12ee829 commit 38a3c7e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/jsx/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { raw } from '../helper/html'
import { escapeToBuffer, resolveCallbackSync, stringBufferToString } from '../utils/html'
import type { HtmlEscaped, HtmlEscapedString, StringBufferWithCallbacks } from '../utils/html'
import { DOM_RENDERER } from './constants'
import { DOM_RENDERER, DOM_MEMO } from './constants'
import type { Context } from './context'
import { createContext, globalContexts, useContext } from './context'
import { domRenderers } from './intrinsic-element/common'
Expand Down Expand Up @@ -346,7 +346,7 @@ export const jsxFn = (
}
}

const shallowEqual = (a: Props, b: Props): boolean => {
export const shallowEqual = (a: Props, b: Props): boolean => {
if (a === b) {
return true
}
Expand Down Expand Up @@ -382,13 +382,21 @@ export const memo = <T>(
): FC<T> => {
let computed: ReturnType<FC<T>> = null
let prevProps: T | undefined = undefined
return ((props) => {
const wrapper: MemorableFC<T> = ((props: T) => {
if (prevProps && !propsAreEqual(prevProps, props)) {
computed = null
}
prevProps = props
return (computed ||= component(props))
}) as FC<T>
}) as MemorableFC<T>

// This function is for toString(), but it can also be used for DOM renderer.
// So, set DOM_MEMO and DOM_RENDERER for DOM renderer.
wrapper[DOM_MEMO] = propsAreEqual
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(wrapper as any)[DOM_RENDERER] = component

return wrapper as FC<T>
}

export const Fragment = ({
Expand Down

0 comments on commit 38a3c7e

Please sign in to comment.