react-native-web icon indicating copy to clipboard operation
react-native-web copied to clipboard

Dimensions.get calls `change` listeners the first time

Open jonathanj opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the issue

All change listeners on Dimensions are called on the first call to Dimensions.get, even though there hasn't been any dimension-related change that might trigger layout events.

Some libraries, notably react-native-popover-view, assume that dimension changes will result in onLayout being called for views, but because no layout has actually changed this will not happen.

Expected behavior

The one-time initialisation probably shouldn't be calling listeners when something is simply reading the dimensions.

This behaviour of RNW is inconsistent with RN, in my testing.

Steps to reproduce

let count = 0;
Dimensions.addEventListener('change', () => { ++count });
assert(count === 0);
Dimsensions.get('window');
assert(count === 1);

Test case

https://codesandbox.io/s/nostalgic-microservice-du4zvs

Additional comments

I'd be happy to do a PR for this, I just wanted to report it in case there was some context about the behaviour that I may have missed, or if there is some nuance to discuss that I'm unaware of.

jonathanj avatar Sep 22 '22 14:09 jonathanj

https://github.com/necolas/react-native-web/commit/59af091fdb4dd91118b105596a28c093426978b0

necolas avatar Sep 23 '22 05:09 necolas

Thanks for the context.

Would it be possible to preserve the improvement introduced by that commit, but avoid notifying listeners when doing the one-time initialisation?

It’s not clear from the content on the commit/PR if notifying listeners is the key part or if it’s about doing the one-time initialisation sooner.

jonathanj avatar Sep 23 '22 06:09 jonathanj

Yeah maybe. Not much context was provided by the PR author

necolas avatar Sep 23 '22 16:09 necolas

@necolas Just want to say I appreciate you resolving this, and so thoroughly too, thank you!

jonathanj avatar Nov 03 '22 05:11 jonathanj