Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit 5d252d7

Browse files
committed
Clear items to show resize timeout when component unmounts
When the window is resized, timeout(s) with setState are set; these need to be cleaned up on unmount to prevent a potential memory leak
1 parent 18d3687 commit 5d252d7

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/Carousel.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
6363
public isInThrottle?: boolean;
6464
public initialY: number;
6565
private transformPlaceHolder: number;
66+
private itemsToShowTimeout: any;
6667
constructor(props: CarouselProps) {
6768
super(props);
6869
this.containerRef = React.createRef();
@@ -301,7 +302,10 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
301302
this.containerRef.current.offsetWidth !== containerWidth
302303
) {
303304
// this is for handling resizing only.
304-
setTimeout(() => {
305+
if (this.itemsToShowTimeout) {
306+
clearTimeout(this.itemsToShowTimeout);
307+
}
308+
this.itemsToShowTimeout = setTimeout(() => {
305309
this.setItemsToShow(true);
306310
}, this.props.transitionDuration || defaultTransitionDuration);
307311
}
@@ -457,6 +461,9 @@ class Carousel extends React.Component<CarouselProps, CarouselInternalState> {
457461
clearInterval(this.autoPlay);
458462
this.autoPlay = undefined;
459463
}
464+
if (this.itemsToShowTimeout) {
465+
clearTimeout(this.itemsToShowTimeout);
466+
}
460467
}
461468
public resetMoveStatus(): void {
462469
this.onMove = false;

0 commit comments

Comments
 (0)