question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[RN 0.58] removeOrientationListener doesn't remove the listener

See original GitHub issue

Hi there, thank for you for this package.

I’m using it in the Login screen of an app I’m making, and I’m having an issue using the removeOrientationListener method. When I debug, I see that it does not remove the subscription, so when I change screens, I get the following warning:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.
ExceptionsManager.js:82
    in Login (created by Context.Consumer)
    in Connect(Login) (at screens.js:43)
    in NetworkConnectivity (at ReduxNetworkProvider.js:47)
    in ReduxNetworkProvider (created by Context.Consumer)
    in Connect(ReduxNetworkProvider) (at screens.js:42)
    in Provider (at screens.js:41)
    in StoreWrapper (at ComponentWrapper.js:29)
    in WrappedComponent (at renderApplication.js:34)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:98)
    in RCTView (at View.js:45)
    in View (at AppContainer.js:115)
    in AppContainer (at renderApplication.js:33)

I’m using RN 0.58.4. The issue is in EventEmitter.js:213, in the removeListener method. This requires that the original handler specified in listenOrientationChange be included in the parameters (right now, an empty function is passed instead) because of this check:

// The subscription may have been removed during this event loop.
// its listener matches the listener in method parameters
if (subscription && subscription.listener === listener) {
  subscription.remove();
}

Perhaps this was a change in recent RN version. I needed a quick fix so I patched up index.js of this library to workaround this problem, like this:

const listenOrientationChange = that => {
  //Save the handler to a property in the Component
  that.orientationChangeHandler = newDimensions => {
    // Retrieve and save new dimensions
    screenWidth = newDimensions.window.width;
    screenHeight = newDimensions.window.height;
  
    // Trigger screen's rerender with a state update of the orientation variable
    that.setState({
      orientation: screenWidth < screenHeight ? 'portrait' : 'landscape'
    });
  }
  
  Dimensions.addEventListener('change', that.orientationChangeHandler);
};

const removeOrientationListener = that => {
  //Retrieve the original handler stored in the Component
  //Requires passing (this) as a parameter to this function, too
  Dimensions.removeEventListener('change', that.orientationChangeHandler);
};

This solved the problem in my case, although a prettier solution should be possible (don’t think it’s okay to mess with the component properties…).

Please let me know if something isn’t clear. I don’t include the code of my Login page because it follows exactly this README example.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
marudycommented, Feb 25, 2019

Hi there @lschuft and thanks for taking the time to write this detailed post.

Sorry I’ve been a bit busy of the past weeks and reply earlier. Will have a look soon and come back with a fix and a new release! 😃

@manelsanz if you had a global listener you would still need to change/set height, width props per screen right? Or is it only the orientation change that you are thinking of not rewriting?

1reaction
miguelnfuertesccommented, Feb 25, 2019

Thank you! @lschuft … it works!

Read more comments on GitHub >

github_iconTop Results From Across the Web

App state in react-native does not remove the listener
However, it seems that AppState.addEventListener doesn't actually return the remove method. The example given in the docs doesn't work too. – ...
Read more >
A react-native module that can listen on orientation changing ...
A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation. (cross-platform support)
Read more >
OrientationListener | Android Developers
Public constructors. OrientationListener; OrientationListener. Public methods. disable; enable; onAccuracyChanged; onOrientationChanged; onSensorChanged.
Read more >
How to (really) remove eventListeners in React
If this check doesn't return true no listener will be removed from the window. But we pass in the exact same function to...
Read more >
Working with App State and Event Listeners in React Native
useEffect 's return function is executed when the component unmounts, giving the component an opportunity to remove the event listener.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found