Skip to content

Commit 2e3de6f

Browse files
authored
Merge pull request #173 from sag1v/stop-autoplay-onunmount
prevent state updates when unmounted
2 parents 9769fcc + f7a9033 commit 2e3de6f

File tree

4 files changed

+67
-24
lines changed

4 files changed

+67
-24
lines changed

demoApp/src/DemoApp.js

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useState, useRef } from "react";
2-
import Carousel from "react-elastic-carousel";
1+
import React, { useState, useRef, useEffect } from "react";
2+
import Carousel from "../../src/react-elastic-carousel/components/Carousel";
33
import styled from "styled-components";
44

55
const Item = styled.div`
@@ -16,7 +16,6 @@ const Item = styled.div`
1616
const Layout = styled.div`
1717
display: flex;
1818
flex-direction: column;
19-
justify-content: center;
2019
align-items: center;
2120
height: 100vh;
2221
`;
@@ -47,8 +46,12 @@ const CheckBox = ({ label, onToggle, ...rest }) => {
4746
);
4847
};
4948

49+
const serverItems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
50+
5051
const DemoApp = () => {
51-
const [items, setItems] = useState([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
52+
const [show, setShow] = useState(true);
53+
const [enableAutoPlay, setEnableAutoPlay] = useState(false);
54+
const [items, setItems] = useState([]);
5255
const [itemsToShow, setItemsToShow] = useState(3);
5356
const [showArrows, setShowArrows] = useState(true);
5457
const [pagination, setPagination] = useState(true);
@@ -68,9 +71,20 @@ const DemoApp = () => {
6871

6972
const goTo = ({ target }) => carouselRef.current.goTo(Number(target.value));
7073

74+
useEffect(() => {
75+
setTimeout(() => {
76+
setItems(serverItems);
77+
}, 2500);
78+
}, []);
79+
7180
return (
7281
<Layout>
7382
<ControlsLayout>
83+
<StyledControlFields>
84+
<button onClick={() => setShow((o) => !o)}>
85+
{`${show ? "Hide" : "Show"} Carousel`}
86+
</button>
87+
</StyledControlFields>
7488
<StyledControlFields>
7589
<button onClick={addItem}>Add Item</button>
7690
<button onClick={removeItem}>Remove Item</button>
@@ -102,18 +116,26 @@ const DemoApp = () => {
102116
checked={verticalMode}
103117
onToggle={setVerticalMode}
104118
/>
119+
<CheckBox
120+
label="Auto Play"
121+
checked={enableAutoPlay}
122+
onToggle={setEnableAutoPlay}
123+
/>
105124
</ControlsLayout>
106-
<Carousel
107-
ref={carouselRef}
108-
verticalMode={verticalMode}
109-
itemsToShow={itemsToShow}
110-
showArrows={showArrows}
111-
pagination={pagination}
112-
>
113-
{items.map((item) => (
114-
<Item key={item}>{item}</Item>
115-
))}
116-
</Carousel>
125+
{show && (
126+
<Carousel
127+
enableAutoPlay={enableAutoPlay}
128+
ref={carouselRef}
129+
verticalMode={verticalMode}
130+
itemsToShow={itemsToShow}
131+
showArrows={showArrows}
132+
pagination={pagination}
133+
>
134+
{items.map((item) => (
135+
<Item key={item}>{item}</Item>
136+
))}
137+
</Carousel>
138+
)}
117139
</Layout>
118140
);
119141
};

demoApp/src/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3-
import DemoApp from './DemoApp';
3+
import DemoApp from "./DemoApp";
44

5-
ReactDOM.render(<DemoApp />, document.getElementById("app"));
5+
ReactDOM.render(
6+
<React.StrictMode>
7+
<DemoApp />
8+
</React.StrictMode>,
9+
document.getElementById("app")
10+
);

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-elastic-carousel",
3-
"version": "0.11.4",
3+
"version": "0.11.5-beta.3",
44
"description": "A flexible and responsive carousel component for react",
55
"author": "sag1v (Sagiv Ben Giat)",
66
"license": "MIT",
@@ -25,7 +25,6 @@
2525
"test:watch": "react-scripts test --env=jsdom",
2626
"prebuild": "yarn run lint:fix",
2727
"build": "rollup -c",
28-
"prepare": "yarn run build",
2928
"build-doc": "docz build",
3029
"deploy-doc": "gh-pages -d demo"
3130
},

src/react-elastic-carousel/components/Carousel.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { pipe, noop, cssPrefix, numberToArray } from "../utils/helpers";
1717
import { Pagination } from "./Pagination";
1818

1919
class Carousel extends React.Component {
20+
isComponentMounted = false;
2021
state = {
2122
rootHeight: 0,
2223
childHeight: 0,
@@ -32,6 +33,7 @@ class Carousel extends React.Component {
3233
};
3334

3435
componentDidMount() {
36+
this.isComponentMounted = true;
3537
this.initResizeObserver();
3638
this.updateActivePage();
3739
this.setPages();
@@ -89,6 +91,8 @@ class Carousel extends React.Component {
8991
}
9092

9193
componentWillUnmount() {
94+
this.isComponentMounted = false;
95+
this.removeAutoPlay();
9296
this.unSubscribeObserver();
9397
}
9498

@@ -125,9 +129,11 @@ class Carousel extends React.Component {
125129
setAutoPlay = () => {
126130
const { autoPlaySpeed } = this.getDerivedPropsFromBreakPoint();
127131
this.autoPlayIntervalId = setInterval(() => {
128-
const { transitioning } = this.state;
129-
if (!transitioning) {
130-
this.slideNext();
132+
if (this.isComponentMounted) {
133+
const { transitioning } = this.state;
134+
if (!transitioning) {
135+
this.slideNext();
136+
}
131137
}
132138
}, autoPlaySpeed);
133139
};
@@ -200,7 +206,11 @@ class Carousel extends React.Component {
200206
// go back from 0ms to whatever set by the user
201207
// We were at 0ms because we wanted to disable animation on resize
202208
// see https://github.com/sag1v/react-elastic-carousel/issues/94
203-
window.requestAnimationFrame(() => this.setState({ transitionMs }));
209+
window.requestAnimationFrame(() => {
210+
if (this.isComponentMounted) {
211+
this.setState({ transitionMs });
212+
}
213+
});
204214
return {
205215
sliderPosition,
206216
activeIndex: newActiveIndex < 0 ? 0 : newActiveIndex
@@ -209,6 +219,10 @@ class Carousel extends React.Component {
209219
};
210220

211221
onSliderResize = sliderNode => {
222+
if (!this.isComponentMounted) {
223+
return;
224+
}
225+
212226
const {
213227
verticalMode,
214228
children,
@@ -271,7 +285,10 @@ class Carousel extends React.Component {
271285
const containerWidth =
272286
newSliderContainerWidth - (initialVerticalMode ? 0 : outerSpacing * 2);
273287

274-
if (this.state.sliderContainerWidth === newSliderContainerWidth) {
288+
if (
289+
!this.isComponentMounted ||
290+
this.state.sliderContainerWidth === newSliderContainerWidth
291+
) {
275292
// prevent infinite loop
276293
return;
277294
}

0 commit comments

Comments
 (0)