Skip to content

Commit 2df0b47

Browse files
feat(jsx): add React-compatible overloads to useRef (#5063)
* 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. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 3bc96ba commit 2df0b47

3 files changed

Lines changed: 43 additions & 14 deletions

File tree

‎src/jsx/dom/index.test.tsx‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,8 +1905,33 @@ describe('DOM', () => {
19051905
})
19061906

19071907
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 overload
1911+
ref.current.set('a', 1)
1912+
expect(ref.current.get('a')).toBe(1)
1913+
})
1914+
1915+
it('types: null initial value returns RefObject<T | null>', () => {
1916+
const ref = useRef<HTMLDivElement>(null)
1917+
expect(ref.current).toBeNull()
1918+
})
1919+
1920+
it('types: undefined initial value returns RefObject<T | undefined>', () => {
1921+
const ref = useRef<number>(undefined)
1922+
expect(ref.current).toBeUndefined()
1923+
ref.current = 1
1924+
expect(ref.current).toBe(1)
1925+
})
1926+
19081927
it('simple', async () => {
1909-
const Input = ({ label, ref }: { label: string; ref: RefObject<HTMLInputElement> }) => {
1928+
const Input = ({
1929+
label,
1930+
ref,
1931+
}: {
1932+
label: string
1933+
ref: RefObject<HTMLInputElement | null>
1934+
}) => {
19101935
return (
19111936
<div>
19121937
<label>{label}</label>
@@ -2015,7 +2040,7 @@ describe('DOM', () => {
20152040
})
20162041

20172042
it('cleanup', async () => {
2018-
const Child = ({ parent }: { parent: RefObject<HTMLElement> }) => {
2043+
const Child = ({ parent }: { parent: RefObject<HTMLElement | null> }) => {
20192044
useEffect(() => {
20202045
return () => {
20212046
parent.current?.setAttribute('data-cleanup', 'true')
@@ -2115,7 +2140,7 @@ describe('DOM', () => {
21152140
})
21162141

21172142
it('cleanup', async () => {
2118-
const Child = ({ parent }: { parent: RefObject<HTMLElement> }) => {
2143+
const Child = ({ parent }: { parent: RefObject<HTMLElement | null> }) => {
21192144
useLayoutEffect(() => {
21202145
return () => {
21212146
parent.current?.setAttribute('data-cleanup', 'true')
@@ -2229,7 +2254,7 @@ describe('DOM', () => {
22292254
})
22302255

22312256
it('cleanup', async () => {
2232-
const Child = ({ parent }: { parent: RefObject<HTMLElement> }) => {
2257+
const Child = ({ parent }: { parent: RefObject<HTMLElement | null> }) => {
22332258
useInsertionEffect(() => {
22342259
return () => {
22352260
parent.current?.setAttribute('data-cleanup', 'true')

‎src/jsx/dom/intrinsic-element/components.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const clearCache = () => {
2222

2323
// this function is exported for testing and should not be used by the user
2424
export const composeRef = <T>(
25-
ref: RefObject<T> | Function | undefined,
25+
ref: RefObject<T | null> | Function | undefined,
2626
cb: (e: T) => void | (() => void)
2727
): ((e: T) => () => void) => {
2828
return useMemo(
@@ -320,7 +320,7 @@ export const form: FC<
320320
PropsWithChildren<{
321321
action?: Function | string
322322
method?: 'get' | 'post'
323-
ref?: RefObject<HTMLFormElement> | ((e: HTMLFormElement | null) => void | (() => void))
323+
ref?: RefObject<HTMLFormElement | null> | ((e: HTMLFormElement | null) => void | (() => void))
324324
}>
325325
> = (props) => {
326326
const { action, ...restProps } = props
@@ -392,7 +392,7 @@ const formActionableElement = (
392392
...props
393393
}: {
394394
formAction?: Function | string
395-
ref?: RefObject<HTMLInputElement> | ((e: HTMLInputElement) => void | (() => void))
395+
ref?: RefObject<HTMLInputElement | null> | ((e: HTMLInputElement) => void | (() => void))
396396
}
397397
) => {
398398
if (typeof formAction === 'function') {

‎src/jsx/hooks/index.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,15 @@ export const useCallback = <T extends Function>(callback: T, deps: readonly unkn
315315
return callback
316316
}
317317

318-
export type RefObject<T> = { current: T | null }
319-
export const useRef = <T>(initialValue: T | null): RefObject<T> => {
318+
export type RefObject<T> = { current: T }
319+
320+
export function useRef<T>(initialValue: T): RefObject<T>
321+
export function useRef<T>(initialValue: T | null): RefObject<T | null>
322+
export function useRef<T>(initialValue: T | undefined): RefObject<T | undefined>
323+
export function useRef<T>(initialValue?: T | null): RefObject<T | null | undefined> {
320324
const buildData = buildDataStack.at(-1) as [unknown, NodeObject]
321325
if (!buildData) {
322-
return { current: initialValue }
326+
return { current: initialValue } as RefObject<T | null | undefined>
323327
}
324328
const [, node] = buildData
325329

@@ -368,21 +372,21 @@ export const useId = (): string => useMemo(() => `:r${(idCounter++).toString(32)
368372
// Define to avoid errors. This hook currently does nothing.
369373
export const useDebugValue = (_value: unknown, _formatter?: (value: unknown) => string): void => {}
370374

371-
export const createRef = <T>(): RefObject<T> => {
375+
export const createRef = <T>(): RefObject<T | null> => {
372376
return { current: null }
373377
}
374378

375379
export const forwardRef = <T, P = {}>(
376-
Component: (props: P, ref?: RefObject<T>) => JSX.Element
377-
): ((props: P & { ref?: RefObject<T> }) => JSX.Element) => {
380+
Component: (props: P, ref?: RefObject<T | null>) => JSX.Element
381+
): ((props: P & { ref?: RefObject<T | null> }) => JSX.Element) => {
378382
return (props) => {
379383
const { ref, ...rest } = props
380384
return Component(rest as P, ref)
381385
}
382386
}
383387

384388
export const useImperativeHandle = <T>(
385-
ref: RefObject<T>,
389+
ref: RefObject<T | null>,
386390
createHandle: () => T,
387391
deps: readonly unknown[]
388392
): void => {

0 commit comments

Comments
 (0)