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

Freezes on fast navigating through screens #77

Open
therobertgrigorian opened this issue Aug 2, 2022 · 2 comments
Open

Freezes on fast navigating through screens #77

therobertgrigorian opened this issue Aug 2, 2022 · 2 comments

Comments

@therobertgrigorian
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

If swipe and navigate to another screen and return back to the screen the swiper deck is frozen intermittently. I suppose the root cause is if we do it fast the component can't register an event in time and we trying to remove the event listener that isn't registered yet.

And one more fix - removeEventListener is deprecated and I replaced it with the remove method.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-deck-swiper/Swiper.js b/node_modules/react-native-deck-swiper/Swiper.js
index edcc140..a122119 100644
--- a/node_modules/react-native-deck-swiper/Swiper.js
+++ b/node_modules/react-native-deck-swiper/Swiper.js
@@ -1,5 +1,5 @@
 import React, { Component } from 'react'
-import { PanResponder, Text, View, Dimensions, Animated } from 'react-native'
+import { PanResponder, Text, View, Dimensions, Animated, InteractionManager } from 'react-native';
 import PropTypes from 'prop-types'
 import isEqual from 'lodash/isEqual'
 
@@ -56,6 +56,7 @@ class Swiper extends Component {
     this._mounted = true
     this._animatedValueX = 0
     this._animatedValueY = 0
+    this._changeEventSubscription = null;
 
     this.state.pan.x.addListener(value => (this._animatedValueX = value.value))
     this.state.pan.y.addListener(value => (this._animatedValueY = value.value))
@@ -82,9 +83,11 @@ class Swiper extends Component {
 
   componentWillUnmount = () => {
     this._mounted = false
-    this.state.pan.x.removeAllListeners()
-    this.state.pan.y.removeAllListeners()
-    Dimensions.removeEventListener('change', this.onDimensionsChange)
+    InteractionManager.runAfterInteractions(() => {
+      this.state.pan.x.removeAllListeners()
+      this.state.pan.y.removeAllListeners()
+      this._changeEventSubscription?.remove()
+    });
   }
 
   getCardStyle = () => {
@@ -110,7 +113,7 @@ class Swiper extends Component {
 
   initializeCardStyle = () => {
     // this.forceUpdate()
-    Dimensions.addEventListener('change', this.onDimensionsChange)
+    this._changeEventSubscription = Dimensions.addEventListener('change', this.onDimensionsChange)
   }
 
   initializePanResponder = () => {

This issue body was partially generated by patch-package.

@dakotawalker
Copy link

Upgrading to 2.0.8 didn't resolve the issue but patching with this diff did. Thanks @therobertgrigorian.

@webraptor
Copy link
Owner

You can open a PR and bump to 2.0.9 with the fix

@webraptor webraptor reopened this Aug 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants