Skip to content
Myr

feat(jsx): add React-compatible overloads to useRef (#5063)

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

* feat(jsx): add React-compatible overloads to useRef Adds overloads so that useRef returns a non-nullable ref when given a non-null initial value, matching React's API. - useRef<T>(initialValue: T): MutableRefObject<T> - useRef<T>(initialValue: T | null): RefObject<T> - useRef<T = undefined>(): MutableRefObject<T | undefined> This is a type-only change; the runtime implementation is unchanged. Closes #5056 * feat(jsx)!: align RefObject and useRef with React 19 - `RefObject<T>` now has non-null mutable `current` - `MutableRefObject<T>` becomes a deprecated alias - `useRef` overloads match React 19; zero-arg form removed in favor of `useRef(undefined)` - `createRef`, `forwardRef`, `useImperativeHandle` updated to use `RefObject<T | null>` for nullable refs BREAKING CHANGE: `RefObject<T>` was `{ current: T | null }`; nullable refs should now be typed as `RefObject<T | null>`. `useRef()` with no argument no longer type-checks; pass `undefined` explicitly. * ci: apply automated fixes * refactor(jsx): remove MutableRefObject alias hono/jsx has never exposed MutableRefObject before, and React 19 marks it as deprecated. Per PR discussion, drop the alias entirely instead of keeping a deprecated re-export. ---------

authored by asahi · committed by GitHub · parent 3bc96ba · Browse files · 3 files+43 added−14 removed
This commit

State

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

Trailers

  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

Diff stats

3 files+43 added−14 removed

  • tests:+29 added−4 removed
  • other:+14 added−10 removed

3 files+43 added−14 removed

modified:src/jsx/dom/intrinsic-element/components.ts+3 added-3 removed
20 unmodified lines
21}2223// this function is exported for testing and should not be used by the user24export const composeRef = <T>(25 ref: RefObject<T> | Function | undefined,25 ref: RefObject<T | null> | Function | undefined,26 cb: (e: T) => void | (() => void)27): ((e: T) => () => void) => {28 return useMemo(29 () => (e: T) => {
289 unmodified lines
319export const form: FC<320 PropsWithChildren<{321 action?: Function | string322 method?: 'get' | 'post'323 ref?: RefObject<HTMLFormElement> | ((e: HTMLFormElement | null) => void | (() => void))323 ref?: RefObject<HTMLFormElement | null> | ((e: HTMLFormElement | null) => void | (() => void))324 }>325> = (props) => {326 const { action, ...restProps } = props327 if (typeof action !== 'function') {
63 unmodified lines
391 formAction,392 ...props393 }: {394 formAction?: Function | string395 ref?: RefObject<HTMLInputElement> | ((e: HTMLInputElement) => void | (() => void))395 ref?: RefObject<HTMLInputElement | null> | ((e: HTMLInputElement) => void | (() => void))396 }397) => {398 if (typeof formAction === 'function') {399 const onClick = useCallback<(ev: MouseEvent) => void>((ev: MouseEvent) => {
39 unmodified lines
modified:src/jsx/dom/index.test.tsx+29 added-4 removed
1903 unmodified lines
1904 })1905 })19061907 describe('useRef', async () => {1908 it('types: non-null initial value returns RefObject<T>', () => {1909 const ref = useRef(new Map<string, number>())1910 // .current is non-nullable; this line would not type-check without the overload1911 ref.current.set('a', 1)1912 expect(ref.current.get('a')).toBe(1)1913 })19141915 it('types: null initial value returns RefObject<T | null>', () => {1916 const ref = useRef<HTMLDivElement>(null)1917 expect(ref.current).toBeNull()1918 })19191920 it('types: undefined initial value returns RefObject<T | undefined>', () => {1921 const ref = useRef<number>(undefined)1922 expect(ref.current).toBeUndefined()1923 ref.current = 11924 expect(ref.current).toBe(1)1925 })19261927 it('simple', async () => {1909 const Input = ({ label, ref }: { label: string; ref: RefObject<HTMLInputElement> }) => {1928 const Input = ({1929 label,1930 ref,1931 }: {1932 label: string1933 ref: RefObject<HTMLInputElement | null>1934 }) => {1935 return (1936 <div>1937 <label>{label}</label>1938 <input ref={ref} />
100 unmodified lines
2039 expect(root.innerHTML).toBe('<div>2</div>')2040 })20412042 it('cleanup', async () => {2018 const Child = ({ parent }: { parent: RefObject<HTMLElement> }) => {2043 const Child = ({ parent }: { parent: RefObject<HTMLElement | null> }) => {2044 useEffect(() => {2045 return () => {2046 parent.current?.setAttribute('data-cleanup', 'true')2047 }
91 unmodified lines
2139 expect(root.innerHTML).toBe('<div>2</div>')2140 })21412142 it('cleanup', async () => {2118 const Child = ({ parent }: { parent: RefObject<HTMLElement> }) => {2143 const Child = ({ parent }: { parent: RefObject<HTMLElement | null> }) => {2144 useLayoutEffect(() => {2145 return () => {2146 parent.current?.setAttribute('data-cleanup', 'true')2147 }
105 unmodified lines
2253 expect(root.innerHTML).toBe('<div>useInsertionEffect,useLayoutEffect</div>')2254 })22552256 it('cleanup', async () => {2232 const Child = ({ parent }: { parent: RefObject<HTMLElement> }) => {2257 const Child = ({ parent }: { parent: RefObject<HTMLElement | null> }) => {2258 useInsertionEffect(() => {2259 return () => {2260 parent.current?.setAttribute('data-cleanup', 'true')2261 }
663 unmodified lines
modified:src/jsx/hooks/index.ts+11 added-7 removed
313 unmodified lines
314 }315 return callback316}317318export type RefObject<T> = { current: T | null }319export const useRef = <T>(initialValue: T | null): RefObject<T> => {318export type RefObject<T> = { current: T }319320export function useRef<T>(initialValue: T): RefObject<T>321export function useRef<T>(initialValue: T | null): RefObject<T | null>322export function useRef<T>(initialValue: T | undefined): RefObject<T | undefined>323export function useRef<T>(initialValue?: T | null): RefObject<T | null | undefined> {324 const buildData = buildDataStack.at(-1) as [unknown, NodeObject]325 if (!buildData) {322 return { current: initialValue }326 return { current: initialValue } as RefObject<T | null | undefined>327 }328 const [, node] = buildData329330 const refArray = (node[DOM_STASH][1][STASH_REF] ||= [])
40 unmodified lines
371372// Define to avoid errors. This hook currently does nothing.373export const useDebugValue = (_value: unknown, _formatter?: (value: unknown) => string): void => {}374371export const createRef = <T>(): RefObject<T> => {375export const createRef = <T>(): RefObject<T | null> => {376 return { current: null }377}378379export const forwardRef = <T, P = {}>(376 Component: (props: P, ref?: RefObject<T>) => JSX.Element377): ((props: P & { ref?: RefObject<T> }) => JSX.Element) => {380 Component: (props: P, ref?: RefObject<T | null>) => JSX.Element381): ((props: P & { ref?: RefObject<T | null> }) => JSX.Element) => {382 return (props) => {383 const { ref, ...rest } = props384 return Component(rest as P, ref)385 }386}387388export const useImperativeHandle = <T>(385 ref: RefObject<T>,389 ref: RefObject<T | null>,390 createHandle: () => T,391 deps: readonly unknown[]392): void => {393 useEffect(() => {
44 unmodified lines