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

[0.65.x] Deprecated Dimensions event listeners #50

Open
efstathiosntonas opened this issue Aug 25, 2021 · 3 comments
Open

[0.65.x] Deprecated Dimensions event listeners #50

efstathiosntonas opened this issue Aug 25, 2021 · 3 comments

Comments

@efstathiosntonas
Copy link

efstathiosntonas commented Aug 25, 2021

Since react-native 0.65.x handling listeners has changed, here's a patch-package file to temp fix only ToastView.js:

diff --git a/node_modules/react-native-smart-tip/toast/ToastView.js b/node_modules/react-native-smart-tip/toast/ToastView.js
index 348e3a4..27f36e7 100644
--- a/node_modules/react-native-smart-tip/toast/ToastView.js
+++ b/node_modules/react-native-smart-tip/toast/ToastView.js
@@ -25,7 +25,7 @@ export default class ToastView extends Component{
         }
 
         // React after 17
-        Dimensions.addEventListener('change', this.onWindowChange);
+       this.dimensionsSubscription = Dimensions.addEventListener('change', this.onWindowChange);
     }
 
     componentDidMount() {
@@ -37,7 +37,7 @@ export default class ToastView extends Component{
             this.liftCycleAnimated.stop()
             this.liftCycleAnimated = undefined
         }
-        Dimensions.removeEventListener('change', this.onWindowChange);
+        this.dimensionsSubscription?.remove();
     }
 
     render() {


@mochixuan for backwards compatibility you could add:

import React from 'react';
const ReactNativeVersion = require('react-native/Libraries/Core/ReactNativeVersion');


constructor(props) {
    super(props);

    const { width, height } = Dimensions.get("window");

    this.state = {
      deviceWidth: width,
      deviceHeight: height,
      animatedValue1: new Animated.Value(0),
      animatedValue2: new Animated.Value(0.2)
    };

    // React after 17
    if (v >= 65) {
      this.dimensionsSubscription = Dimensions.addEventListener(
        "change",
        this.onWindowChange
      );
    } else {
      Dimensions.addEventListener("change", this.onWindowChange);
    }
  }

  componentWillUnmount() {
    if (this.liftCycleAnimated) {
      this.liftCycleAnimated.stop();
      this.liftCycleAnimated = undefined;
    }

    const v = parseInt(
      ReactNativeVersion.version.major + ReactNativeVersion.version.minor,
      10
    );

    if (v >= 65) {
      this.dimensionsSubscription?.remove();
    } else {
      Dimensions.removeEventListener("change", this.onWindowChange);
    }
  }


not tested ^^, just a suggestion, this should apply to all Views, not only ToastView 😄

@mochixuan
Copy link
Owner

Thank you very much for your suggestion, if there is a compatibility problem, I will make it compatible in the future

@YongZL
Copy link

YongZL commented Oct 19, 2022

@efstathiosntonas Thank you for solving my problem

flyskywhy added a commit to flyskywhy/react-native-smart-tip that referenced this issue Sep 19, 2024
@flyskywhy
Copy link

Because no official new version for years, I have to create @flyskywhy/react-native-smart-tip to solve this issue.

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

4 participants