Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compat: Improve React compatibility for Ref type. #4403

Merged
merged 2 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compat/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
rschristian marked this conversation as resolved.
Show resolved Hide resolved
export import SetStateAction = _hooks.StateUpdater;
export import useCallback = _hooks.useCallback;
export import useContext = _hooks.useContext;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions compat/test/ts/forward-ref.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ export const Bar = React.forwardRef<HTMLDivElement, { children: any }>(
return <div ref={ref}>{props.children}</div>;
}
);

export const baz = (
ref: React.ForwardedRef<HTMLElement>
): React.Ref<HTMLElement> => ref;
9 changes: 3 additions & 6 deletions hooks/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorInfo, PreactContext, Ref as PreactRef } from '../..';
import { ErrorInfo, PreactContext, Ref, RefObject } from '../..';

type Inputs = ReadonlyArray<unknown>;

Expand Down Expand Up @@ -52,9 +52,6 @@ export function useReducer<S, A, I>(

/** @deprecated Use the `Ref` type instead. */
type PropRef<T> = MutableRef<T>;
interface Ref<T> {
readonly current: T | null;
}

interface MutableRef<T> {
current: T;
Expand All @@ -70,7 +67,7 @@ interface MutableRef<T> {
* @param initialValue the initial value to store in the ref object
*/
export function useRef<T>(initialValue: T): MutableRef<T>;
export function useRef<T>(initialValue: T | null): Ref<T>;
export function useRef<T>(initialValue: T | null): RefObject<T>;
export function useRef<T = undefined>(): MutableRef<T | undefined>;

type EffectCallback = () => void | (() => void);
Expand All @@ -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<T, R extends T>(
ref: PreactRef<T>,
ref: Ref<T>,
create: () => R,
inputs?: Inputs
): void;
Expand Down
Loading