Skip to content

Commit 249f589

Browse files
authored
fix(js): handle the component unmounting (#340)
* Fixes #339 - Handle the component unmounting If the component unmounts immediately after mounting, then the call to requestAnimationFrame will attempt to operate on the unmounted component. We can prevent this by ensuring we cleanup the callback on unmounting. * Default value to undefined, rather than 0
1 parent 24f0405 commit 249f589

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/PagerView.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,19 @@ import { getViewManagerConfig, PagerViewViewManager } from './PagerViewNative';
5555

5656
export class PagerView extends React.Component<PagerViewProps> {
5757
private isScrolling = false;
58+
private animationFrameRequestId?: number;
5859
private PagerView = React.createRef<typeof PagerViewViewManager>();
5960

61+
componentWillUnmount() {
62+
if (this.animationFrameRequestId !== undefined) {
63+
cancelAnimationFrame(this.animationFrameRequestId);
64+
}
65+
}
66+
6067
componentDidMount() {
6168
// On iOS we do it directly on the native side
6269
if (Platform.OS === 'android' && this.props.initialPage !== undefined) {
63-
requestAnimationFrame(() => {
70+
this.animationFrameRequestId = requestAnimationFrame(() => {
6471
if (this.props.initialPage !== undefined) {
6572
this.setPageWithoutAnimation(this.props.initialPage);
6673
}

0 commit comments

Comments
 (0)