Skip to content
Myr

feat(jsx): more react staff (#2197)

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

* feat(jsx): Introduce useReducer hook * feat(jsx): add dummy useDebugValue hook * feat(jsx/dom): add createElement function * feat(jsx): export all staffs from jsx as default * chore: denoify * refactor(jsx/dom): export jsx as an alias of createElement * chore: denoify

authored by Taku Amano · committed by GitHub · parent 30b33b0 · Browse files · 9 files+484 added−31 removed
This commit

State

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

Diff stats

9 files+484 added−31 removed

  • tests:+170 added−3 removed
  • other:+314 added−28 removed

9 files+484 added−31 removed

modified:deno_dist/jsx/dom/index.ts+79 added-9 removed
modified:deno_dist/jsx/hooks/index.ts+18 added
202 unmodified lines
203 },204 ])205}206207export const useReducer = <T, A>(208 reducer: (state: T, action: A) => T,209 initialArg: T,210 init?: (initialState: T) => T211): [T, (action: A) => void] => {212 const [state, setState] = useState(() => (init ? init(initialArg) : initialArg))213 return [214 state,215 (action: A) => {216 setState((state) => reducer(state, action))217 },218 ]219}220221const useEffectCommon = (222 index: number,223 effect: () => void | (() => void),224 deps?: readonly unknown[]
112 unmodified lines
337 memoArray[hookIndex] = [factory(), deps]338 }339 return memoArray[hookIndex][0] as T340}341342// Define to avoid errors. This hook currently does nothing.343// eslint-disable-next-line @typescript-eslint/no-unused-vars344export const useDebugValue = (value: unknown, formatter?: (value: unknown) => string): void => {}
modified:deno_dist/jsx/index.ts+60 added-5 removed
modified:src/jsx/dom/index.test.tsx+75 added-2 removed
1 unmodified line
2import type { FC } from '..'3// run tests by old style jsx default4// hono/jsx/jsx-runtime and hono/jsx/dom/jsx-runtime are tested in their respective settings5// eslint-disable-next-line @typescript-eslint/no-unused-vars6import { jsx, Fragment } from '..'6import { jsx, Fragment, createElement } from '..'7import DefaultExport from '..'8import type { RefObject } from '../hooks'9import { useState, useEffect, useLayoutEffect, useCallback, useRef, useMemo } from '../hooks'10import { memo, isValidElement, cloneElement } from '.'10import { render, cloneElement as cloneElementForDom } from '.'11import { render, createElement as createElementForDom, cloneElement as cloneElementForDom } from '.'1213describe('DOM', () => {14 beforeAll(() => {15 global.requestAnimationFrame = (cb) => setTimeout(cb)
1257 unmodified lines
1273 expect(isValidElement({})).toBe(false)1274 })1275 })12761277 describe('createElement', () => {1278 it('simple', async () => {1279 const App = () => {1280 const [count, setCount] = useState(0)1281 return (1282 <div>{createElement('p', { onClick: () => setCount(count + 1) }, String(count))}</div>1283 )1284 }1285 const app = <App />1286 render(app, root)1287 expect(root.innerHTML).toBe('<div><p>0</p></div>')1288 root.querySelector('p')?.click()1289 await Promise.resolve()1290 expect(root.innerHTML).toBe('<div><p>1</p></div>')1291 })1292 })12931294 describe('dom-specific createElement', () => {1295 it('simple', async () => {1296 const App = () => {1297 const [count, setCount] = useState(0)1298 return <div>{createElementForDom('p', { onClick: () => setCount(count + 1) }, count)}</div>1299 }1300 const app = <App />1301 render(app, root)1302 expect(root.innerHTML).toBe('<div><p>0</p></div>')1303 root.querySelector('p')?.click()1304 await Promise.resolve()1305 expect(root.innerHTML).toBe('<div><p>1</p></div>')1306 })1307 })13081309 describe('cloneElement', () => {1310 it('simple', async () => {1311 const App = () => {1312 const [count, setCount] = useState(0)
82 unmodified lines
1395 expect(createElementNSSpy).toHaveBeenCalledWith('http://www.w3.org/1998/Math/MathML', 'mrow')1396 })1397 })1398})13991400describe('jsx', () => {1401 it('exported as an alias of createElement', () => {1402 expect(jsx).toBeDefined()1403 expect(jsx('div', {}, 'Hello')).toBeInstanceOf(Object)1404 })1405})14061407describe('default export', () => {1408 ;[1409 'memo',1410 'Fragment',1411 'isValidElement',1412 'createElement',1413 'cloneElement',1414 'ErrorBoundary',1415 'createContext',1416 'useContext',1417 'useState',1418 'useEffect',1419 'useRef',1420 'useCallback',1421 'useReducer',1422 'useDebugValue',1423 'use',1424 'startTransition',1425 'useTransition',1426 'useDeferredValue',1427 'startViewTransition',1428 'useViewTransition',1429 'useMemo',1430 'useLayoutEffect',1431 'Suspense',1432 ].forEach((key) => {1433 it(key, () => {1434 // eslint-disable-next-line @typescript-eslint/no-explicit-any1435 expect((DefaultExport as any)[key]).toBeDefined()1436 })1437 })1438})
modified:src/jsx/dom/index.ts+79 added-9 removed
modified:src/jsx/hooks/dom.test.tsx+61 added
5 unmodified lines
6import { Suspense } from '../dom'7import { render } from '../dom'8import {9 useState,10 useReducer,11 use,12 startTransition,13 useTransition,14 useDeferredValue,15 startViewTransition,16 useViewTransition,17 useDebugValue,18} from '.'1920describe('useReducer()', () => {21 beforeAll(() => {22 global.requestAnimationFrame = (cb) => setTimeout(cb)23 })2425 let dom: JSDOM26 let root: HTMLElement27 beforeEach(() => {28 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {29 runScripts: 'dangerously',30 })31 global.document = dom.window.document32 global.HTMLElement = dom.window.HTMLElement33 global.Text = dom.window.Text34 root = document.getElementById('root') as HTMLElement35 })3637 it('simple', async () => {38 const App = () => {39 const [state, dispatch] = useReducer((state: number, action: number) => state + action, 0)40 return (41 <div>42 <button onClick={() => dispatch(1)}>{state}</button>43 </div>44 )45 }46 render(<App />, root)47 expect(root.innerHTML).toBe('<div><button>0</button></div>')48 root.querySelector('button')?.click()49 await Promise.resolve()50 expect(root.innerHTML).toBe('<div><button>1</button></div>')51 })52})5354describe('startTransition()', () => {55 beforeAll(() => {56 global.requestAnimationFrame = (cb) => setTimeout(cb)57 })
421 unmodified lines
479 expect(root.innerHTML).toBe('<div><button>1</button></div>')480 expect(called).toBe(3)481 })482})483484describe('useDebugValue()', () => {485 let dom: JSDOM486 let root: HTMLElement487 beforeEach(() => {488 dom = new JSDOM('<html><body><div id="root"></div></body></html>', {489 runScripts: 'dangerously',490 })491 global.document = dom.window.document492 global.HTMLElement = dom.window.HTMLElement493 global.Text = dom.window.Text494 root = document.getElementById('root') as HTMLElement495 })496497 it('simple', () => {498 const spy = vi.fn()499 const App = () => {500 useDebugValue('hello', spy)501 return <div />502 }503 render(<App />, root)504 expect(root.innerHTML).toBe('<div></div>')505 expect(spy).not.toBeCalled()506 })507})
modified:src/jsx/hooks/index.ts+18 added
202 unmodified lines
203 },204 ])205}206207export const useReducer = <T, A>(208 reducer: (state: T, action: A) => T,209 initialArg: T,210 init?: (initialState: T) => T211): [T, (action: A) => void] => {212 const [state, setState] = useState(() => (init ? init(initialArg) : initialArg))213 return [214 state,215 (action: A) => {216 setState((state) => reducer(state, action))217 },218 ]219}220221const useEffectCommon = (222 index: number,223 effect: () => void | (() => void),224 deps?: readonly unknown[]
112 unmodified lines
337 memoArray[hookIndex] = [factory(), deps]338 }339 return memoArray[hookIndex][0] as T340}341342// Define to avoid errors. This hook currently does nothing.343// eslint-disable-next-line @typescript-eslint/no-unused-vars344export const useDebugValue = (value: unknown, formatter?: (value: unknown) => string): void => {}
modified:src/jsx/index.test.tsx+34 added-1 removed
2 unmodified lines
3import { html } from '../helper/html'4import { Hono } from '../hono'5import { Suspense, renderToReadableStream } from './streaming'6// eslint-disable-next-line @typescript-eslint/no-unused-vars7import { jsx, memo, Fragment, createContext, useContext } from '.'7import DefaultExport, { jsx, memo, Fragment, createContext, useContext } from '.'8import type { Context, FC, PropsWithChildren } from '.'910interface SiteData {11 title: string
707 unmodified lines
719 expect(nextRequest.toString()).toBe('<span>light</span>')720 })721 })722})723724describe('default export', () => {725 ;[726 'memo',727 'Fragment',728 'isValidElement',729 'createElement',730 'cloneElement',731 'ErrorBoundary',732 'createContext',733 'useContext',734 'useState',735 'useEffect',736 'useRef',737 'useCallback',738 'useReducer',739 'useDebugValue',740 'use',741 'startTransition',742 'useTransition',743 'useDeferredValue',744 'startViewTransition',745 'useViewTransition',746 'useMemo',747 'useLayoutEffect',748 'Suspense',749 ].forEach((key) => {750 it(key, () => {751 // eslint-disable-next-line @typescript-eslint/no-explicit-any752 expect((DefaultExport as any)[key]).toBeDefined()753 })754 })755})
modified:src/jsx/index.ts+60 added-5 removed
1export { jsx, memo, Fragment, isValidElement, cloneElement } from './base'2export { ErrorBoundary } from './components'3export { Suspense } from './streaming'4export {1import { jsx, memo, Fragment, isValidElement, cloneElement } from './base'2import { ErrorBoundary } from './components'3import { createContext, useContext } from './context'4import {5 useState,6 useEffect,7 useRef,8 useCallback,
4 unmodified lines
13 startViewTransition,14 useViewTransition,15 useMemo,16 useLayoutEffect,17 useReducer,18 useDebugValue,19} from './hooks'18export { createContext, useContext } from './context'20import { Suspense } from './streaming'2122export {23 jsx,24 memo,25 Fragment,26 isValidElement,27 jsx as createElement,28 cloneElement,29 ErrorBoundary,30 createContext,31 useContext,32 useState,33 useEffect,34 useRef,35 useCallback,36 useReducer,37 useDebugValue,38 use,39 startTransition,40 useTransition,41 useDeferredValue,42 startViewTransition,43 useViewTransition,44 useMemo,45 useLayoutEffect,46 Suspense,47}4849export default {50 memo,51 Fragment,52 isValidElement,53 createElement: jsx,54 cloneElement,55 ErrorBoundary,56 createContext,57 useContext,58 useState,59 useEffect,60 useRef,61 useCallback,62 useReducer,63 useDebugValue,64 use,65 startTransition,66 useTransition,67 useDeferredValue,68 startViewTransition,69 useViewTransition,70 useMemo,71 useLayoutEffect,72 Suspense,73}7475// TODO: change to `export type *` after denoify bug is fixed76// https://github.com/garronej/denoify/issues/12477export * from './types'