Skip to content
Myr

feat(jsx/dom): Improve compatibility React (useCallback, React.StrictMode) (#2944)

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

* fix(jsx/hooks): fix useCallback type Fixed problem where callbacks that take arguments could not be specified Like React's useCallback https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8c57769f1fb6a5982e2ba2c83219131a023e67e8/types/react/index.d.ts#L2055 * feat(jsx): export StrictMode as an alias of Fragment

authored by Taku Amano · committed by GitHub · parent c910923 · Browse files · 5 files+45 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

5 files+45 added−5 removed

  • tests:+40 added−1 removed
  • other:+5 added−4 removed

5 files+45 added−5 removed

modified:src/jsx/dom/index.test.tsx+26 added
1678 unmodified lines
1679 await Promise.resolve()1680 expect(root.innerHTML).toBe('<div><p>0</p><button>+</button><p>1</p><button>+</button></div>')1681 expect(callbackSet.size).toBe(1)1682 })16831684 it('deferent callbacks', async () => {1685 const callbackSet = new Set<Function>()1686 const Counter = () => {1687 const [count, setCount] = useState(0)1688 const double = useCallback((input: number): number => {1689 return input * 21690 }, [])1691 callbackSet.add(double)1692 return (1693 <div>1694 <p>{double(count)}</p>1695 <button onClick={() => setCount((c) => c + 1)}>+</button>1696 </div>1697 )1698 }1699 const app = <Counter />1700 render(app, root)1701 expect(root.innerHTML).toBe('<div><p>0</p><button>+</button></div>')1702 const button = root.querySelector('button') as HTMLButtonElement1703 button.click()1704 await Promise.resolve()1705 expect(root.innerHTML).toBe('<div><p>2</p><button>+</button></div>')1706 expect(callbackSet.size).toBe(1)1707 })1708 })17091710 describe('useMemo', () => {1711 it('simple', async () => {
475 unmodified lines
2187 'Suspense',2188 'Fragment',2189 'flushSync',2190 'createPortal',2191 'StrictMode',2192 ].forEach((key) => {2193 it(key, () => {2194 // eslint-disable-next-line @typescript-eslint/no-explicit-any2195 expect((DefaultExport as any)[key]).toBeDefined()
3 unmodified lines
modified:src/jsx/dom/index.ts+2 added
modified:src/jsx/hooks/index.ts+1 added-4 removed
267 unmodified lines
268 effect: () => void | (() => void),269 deps?: readonly unknown[]270): void => useEffectCommon(4, effect, deps)271272export const useCallback = <T extends (...args: unknown[]) => unknown>(273 callback: T,274 deps: readonly unknown[]275): T => {272export const useCallback = <T extends Function>(callback: T, deps: readonly unknown[]): T => {273 const buildData = buildDataStack.at(-1) as [unknown, NodeObject]274 if (!buildData) {275 return callback276 }
149 unmodified lines
modified:src/jsx/index.test.tsx+14 added-1 removed
1 unmodified line
2/* eslint-disable @typescript-eslint/no-explicit-any */3import { html } from '../helper/html'4import { Hono } from '../hono'5import { Suspense, renderToReadableStream } from './streaming'6import DefaultExport, { Fragment, createContext, memo, useContext, version } from '.'6import DefaultExport, { Fragment, StrictMode, createContext, memo, useContext, version } from '.'7import type { Context, FC, PropsWithChildren } from '.'89interface SiteData {10 title: string
520 unmodified lines
531 expect(template.toString()).toBe('')532 })533})534535describe('StrictMode', () => {536 it('Should render children', () => {537 const template = (538 <StrictMode>539 <p>1</p>540 <p>2</p>541 </StrictMode>542 )543 expect(template.toString()).toBe('<p>1</p><p>2</p>')544 })545})546547describe('Context', () => {548 let ThemeContext: Context<string>549 let Consumer: FC550 let ErrorConsumer: FC
226 unmodified lines
777 'useMemo',778 'useLayoutEffect',779 'useInsertionEffect',780 'Suspense',781 'StrictMode',782 ].forEach((key) => {783 it(key, () => {784 // eslint-disable-next-line @typescript-eslint/no-explicit-any785 expect((DefaultExport as any)[key]).toBeDefined()
3 unmodified lines
modified:src/jsx/index.ts+2 added
35 unmodified lines
36 reactAPICompatVersion as version,37 jsx,38 memo,39 Fragment,40 Fragment as StrictMode,41 isValidElement,42 jsx as createElement,43 cloneElement,44 ErrorBoundary,
27 unmodified lines
72export default {73 version: reactAPICompatVersion,74 memo,75 Fragment,76 StrictMode: Fragment,77 isValidElement,78 createElement: jsx,79 cloneElement,80 ErrorBoundary,
29 unmodified lines