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

fix: ref of the responsive container #4008

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Changes from 1 commit
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
24 changes: 20 additions & 4 deletions src/component/ResponsiveContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface Props {
onResize?: (width: number, height: number) => void;
}

export const ResponsiveContainer = forwardRef<HTMLDivElement, Props>(
export const ResponsiveContainer = forwardRef<HTMLDivElement | { current: HTMLDivElement }, Props>(
(
{
aspect,
Expand Down Expand Up @@ -68,9 +68,25 @@ export const ResponsiveContainer = forwardRef<HTMLDivElement, Props>(
const containerRef = useRef<HTMLDivElement>(null);
const onResizeRef = useRef<Props['onResize']>();
onResizeRef.current = onResize;
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
useImperativeHandle(ref, () => containerRef);
useImperativeHandle(ref, () => {
return Object.assign(containerRef.current, {
current: new Proxy(containerRef.current, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this returns a proxy object which will break users that currently have ref.current.current int their code

    React.useEffect(() => {
      if (ref) {
        console.log(ref.current.current);
        console.log(ref.current.current.children);
      }
    }, [ref]);

This works in TypeScript, but it does not execute because current.current is a proxy object

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ckifer Could you please try it once again???

get: (target, prop, receiver) => {
// eslint-disable-next-line no-console
console.warn('The usage of ref.current.current is deprecated and will no longer be supported.');

const value = Reflect.get(target, prop, receiver);
if (typeof value === 'function') {
// eslint-disable-next-line func-names
return function (...args: unknown[]) {
return value.apply(this === receiver ? target : this, args);
};
}
return value;
},
}),
});
});

const [sizes, setSizes] = useState<{
containerWidth: number;
Expand Down