Skip to content

Commit

Permalink
Guard against potential state update after unmount (caused by setTime…
Browse files Browse the repository at this point in the history
…out when using ResizeObserver)
  • Loading branch information
bvaughn committed Jun 1, 2023
1 parent d9c4e86 commit 827f0b6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/AutoSizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class AutoSizer extends Component<Props, State> {
_detectElementResize: DetectElementResize | null = null;
_parentNode: HTMLElement | null = null;
_resizeObserver: ResizeObserver | null = null;
_timeoutId: number | null = null;

componentDidMount() {
const { nonce } = this.props;
Expand All @@ -50,7 +51,7 @@ export class AutoSizer extends Component<Props, State> {
// Guard against "ResizeObserver loop limit exceeded" error;
// could be triggered if the state update causes the ResizeObserver handler to run long.
// See https://github.com/bvaughn/react-virtualized-auto-sizer/issues/55
setTimeout(this._onResize, 0);
this._timeoutId = setTimeout(this._onResize, 0);
});
this._resizeObserver.observe(this._parentNode);
} else {
Expand All @@ -75,6 +76,10 @@ export class AutoSizer extends Component<Props, State> {
);
}

if (this._timeoutId !== null) {
clearTimeout(this._timeoutId);
}

if (this._resizeObserver) {
this._resizeObserver.observe(this._parentNode);
this._resizeObserver.disconnect();
Expand Down Expand Up @@ -141,6 +146,8 @@ export class AutoSizer extends Component<Props, State> {
}

_onResize = () => {
this._timeoutId = null;

const { disableHeight, disableWidth, onResize } = this
.props as HeightAndWidthProps;

Expand Down

0 comments on commit 827f0b6

Please sign in to comment.