From 11c953404c00fa977e26dd88d342ccd521b26ec0 Mon Sep 17 00:00:00 2001 From: Max Brieiev Date: Fri, 31 May 2024 20:11:45 +0300 Subject: [PATCH 1/2] compat: Improve React compatibility for `Ref` type. The commit improves React compatibility by re-exporting `Ref` from core instead of hooks. --- compat/src/index.d.ts | 2 +- compat/test/ts/forward-ref.tsx | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/compat/src/index.d.ts b/compat/src/index.d.ts index 810629c199..a71a21564c 100644 --- a/compat/src/index.d.ts +++ b/compat/src/index.d.ts @@ -18,7 +18,6 @@ declare namespace React { export import PropRef = _hooks.PropRef; export import Reducer = _hooks.Reducer; export import Dispatch = _hooks.Dispatch; - export import Ref = _hooks.Ref; export import SetStateAction = _hooks.StateUpdater; export import useCallback = _hooks.useCallback; export import useContext = _hooks.useContext; @@ -50,6 +49,7 @@ declare namespace React { export import ComponentClass = preact.ComponentClass; export import FC = preact.FunctionComponent; export import createContext = preact.createContext; + export import Ref = preact.Ref; export import createRef = preact.createRef; export import Fragment = preact.Fragment; export import createElement = preact.createElement; diff --git a/compat/test/ts/forward-ref.tsx b/compat/test/ts/forward-ref.tsx index 9a920cb05c..25d0694079 100644 --- a/compat/test/ts/forward-ref.tsx +++ b/compat/test/ts/forward-ref.tsx @@ -24,3 +24,7 @@ export const Bar = React.forwardRef( return
{props.children}
; } ); + +export const baz = ( + ref: React.ForwardedRef +): React.Ref => ref; From 92fd386204efed31a8f5fb3cc596652bd42227ef Mon Sep 17 00:00:00 2001 From: Max Brieiev Date: Sat, 1 Jun 2024 16:25:39 +0300 Subject: [PATCH 2/2] compat: Use `Ref` and `RefObject` types from core in hooks. --- hooks/src/index.d.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/hooks/src/index.d.ts b/hooks/src/index.d.ts index 3ec8999ca6..7bb441edb2 100644 --- a/hooks/src/index.d.ts +++ b/hooks/src/index.d.ts @@ -1,4 +1,4 @@ -import { ErrorInfo, PreactContext, Ref as PreactRef } from '../..'; +import { ErrorInfo, PreactContext, Ref, RefObject } from '../..'; type Inputs = ReadonlyArray; @@ -52,9 +52,6 @@ export function useReducer( /** @deprecated Use the `Ref` type instead. */ type PropRef = MutableRef; -interface Ref { - readonly current: T | null; -} interface MutableRef { current: T; @@ -70,7 +67,7 @@ interface MutableRef { * @param initialValue the initial value to store in the ref object */ export function useRef(initialValue: T): MutableRef; -export function useRef(initialValue: T | null): Ref; +export function useRef(initialValue: T | null): RefObject; export function useRef(): MutableRef; type EffectCallback = () => void | (() => void); @@ -92,7 +89,7 @@ type CreateHandle = () => object; * @param inputs If present, effect will only activate if the values in the list change (using ===). */ export function useImperativeHandle( - ref: PreactRef, + ref: Ref, create: () => R, inputs?: Inputs ): void;