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

Issue #237 - Do not call onResize during setSize updater function in createNotifier #239

Merged
merged 2 commits into from
Apr 9, 2023
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
25 changes: 19 additions & 6 deletions src/ResizeDetector.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import React, { PureComponent, isValidElement, cloneElement, createRef, ReactNode, ReactElement } from 'react';
import React, {
PureComponent,
isValidElement,
cloneElement,
createRef,
ReactNode,
ReactElement,
MutableRefObject
} from 'react';
import { findDOMNode } from 'react-dom';

import { patchResizeHandler, isFunction, isSSR, isDOMElement, createNotifier } from './utils';
Expand All @@ -13,6 +21,11 @@ class ResizeDetector<ElementT extends HTMLElement = HTMLElement> extends PureCom
observableElement;
resizeHandler;
resizeObserver;
/**
* To access the current size in the ResizeObserver without having to recreate it each time size updates.
*/
private readonly sizeRef: MutableRefObject<ReactResizeDetectorDimensions>;

constructor(props: ResizeDetectorProps<ElementT>) {
super(props);

Expand All @@ -22,6 +35,9 @@ class ResizeDetector<ElementT extends HTMLElement = HTMLElement> extends PureCom
width: undefined,
height: undefined
};
this.sizeRef = {
current: this.state
};

this.skipOnMount = skipOnMount;
this.targetRef = createRef();
Expand All @@ -41,6 +57,7 @@ class ResizeDetector<ElementT extends HTMLElement = HTMLElement> extends PureCom

componentDidUpdate(): void {
this.attachObserver();
this.sizeRef.current = this.state;
}

componentWillUnmount(): void {
Expand Down Expand Up @@ -124,11 +141,7 @@ class ResizeDetector<ElementT extends HTMLElement = HTMLElement> extends PureCom

if (!handleWidth && !handleHeight) return;

const notifyResize = createNotifier(
setStateFunc => this.setState(setStateFunc, () => onResize?.(this.state.width, this.state.height)),
handleWidth,
handleHeight
);
const notifyResize = createNotifier(onResize, this.sizeRef, this.setState.bind(this), handleWidth, handleHeight);

entries.forEach(entry => {
const { width, height } = (entry && entry.contentRect) || {};
Expand Down
22 changes: 14 additions & 8 deletions src/useResizeDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ function useResizeDetector<T extends HTMLElement = any>({

// this is a callback that will be called every time the ref is changed
// we call setState inside to trigger rerender

const onRefChange: OnRefChangeType = useCallback((node: T | null) => {
setRefElement(node);
}, []);
if (node !== refElement) {
setRefElement(node);
}
}, [refElement]);

// adding `current` to make it compatible with useRef shape
onRefChange.current = refElement;

Expand All @@ -60,6 +64,12 @@ function useResizeDetector<T extends HTMLElement = any>({
};
}, []);

/**
* To access the current size in the ResizeObserver without having to recreate it each time size updates.
*/
const sizeRef = useRef(size);
sizeRef.current = size;

useEffect(() => {
if (!handleWidth && !handleHeight) return;

Expand All @@ -68,7 +78,7 @@ function useResizeDetector<T extends HTMLElement = any>({
return;
}

const notifyResize = createNotifier(setSize, handleWidth, handleHeight);
const notifyResize = createNotifier(onResize, sizeRef, setSize, handleWidth, handleHeight);

const resizeCallback: ResizeObserverCallback = (entries: ResizeObserverEntry[]) => {
if (!handleWidth && !handleHeight) return;
Expand Down Expand Up @@ -96,11 +106,7 @@ function useResizeDetector<T extends HTMLElement = any>({
resizeObserver.disconnect();
(resizeHandler.current as DebouncedFunc<ResizeObserverCallback>).cancel?.();
};
}, [refreshMode, refreshRate, refreshOptions, handleWidth, handleHeight, observerOptions, refElement]);

useEffect(() => {
onResize?.(size.width, size.height);
}, [size]);
}, [refreshMode, refreshRate, refreshOptions, handleWidth, handleHeight, observerOptions, onResize, refElement]);

return { ref: onRefChange, ...size };
}
Expand Down
31 changes: 18 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,27 @@ export const isDOMElement = (element: unknown): boolean =>

export const createNotifier =
(
onResize: Props['onResize'],
sizeRef: Readonly<React.MutableRefObject<ReactResizeDetectorDimensions>>,
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need this additional variable? Can't we simply compare width and height to the current state?

Copy link
Author

Choose a reason for hiding this comment

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

I stored the current state inside a ref so that we can access its value within the effect without having to declare it as a dependency and recreate the ResizeObserver every time it changes. It's kind of a hacky approach to avoid stale values or triggering the effect too often

Or did you mean using the setState((prev) => { ... }); pattern?

Copy link
Owner

Choose a reason for hiding this comment

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

I see. That's what you initially did and then refactored to the current solution...

Thank you! I'm going to merge the PR

setSize: React.Dispatch<React.SetStateAction<ReactResizeDetectorDimensions>>,
handleWidth: boolean,
handleHeight: boolean
) =>
({ width, height }: ReactResizeDetectorDimensions): void => {
setSize(prev => {
if (prev.width === width && prev.height === height) {
// skip if dimensions haven't changed
return prev;
}

if ((prev.width === width && !handleHeight) || (prev.height === height && !handleWidth)) {
// process `handleHeight/handleWidth` props
return prev;
}

return { width, height };
});
const { current: prev } = sizeRef;

if (prev.width === width && prev.height === height) {
// skip if dimensions haven't changed
return;
}

if ((prev.width === width && !handleHeight) || (prev.height === height && !handleWidth)) {
// process `handleHeight/handleWidth` props
return;
}

onResize?.(width, height);

const newSize = { width, height };
setSize(newSize);
};