react-native: RCTUIManager.measure not working on Android

Description

RCTUIManager.measure is not working on Android. Trying this.refs.myRef.measure() doesn’t give any results either.

Reproduction

import { findNodeHandle } from 'react-native';
const RCTUIManager = require('NativeModules').UIManager;

RCTUIManager.measure(findNodeHandle(this.refs.myRef), (x, y, width, height, pageX, pageY) => {
      // Returns valid values on iOS but undefined on Android
});

Additional Information

  • React Native version: 0.42.0
  • Platform: Android
  • Operating System: MacOS
  • Dev tools: Android Studio 2.3

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 17 (6 by maintainers)

Most upvoted comments

So style={{opacity: 1}} is what I used until I understood the issue.

Collapsable property is explained in https://facebook.github.io/react-native/docs/view.html#collapsable

Views that are only used to layout their children or otherwise don’t draw anything may be automatically removed from the native hierarchy as an optimization. Set this property to false to disable this optimization and ensure that this View exists in the native view hierarchy.

However, some of UIManager functions such as measure expect to find a native view; and when it can’t it returns undefined values instead of throwing an error. Really it would be better to throw an error. React unfortunately is not intelligent to know you would be measuring the view in the first place.

Putting an explicit style is one way to disable the optimization; because now the view has to be created with special drawing properties. But the real fix is putting collapsable={false} on the view you will be measuring to disable the optimization by the documentation.

Adding style={{opacity: 1}} did the trick. Thanks a lot!

Referring to your comment in #9382, what exactly do you mean by collapsable property?

Now I always force it to be created with collapsable property.

Although an annoyance, should I just follow your lead and close out this issue?

Also, unless you have infinite scrolling and/or very long lists with lots of images (basically heavy-weight rendering needs) I wouldn’t worry too much about clipping views.

@itinance That’s a shame. I was not aware it wouldn’t get further bug fixes. You can look at if the new alternative FlatList has a way to do the same thing, possible through onViewableItemsChanged. I never used FlatList so I wouldn’t be able to help you there. Good luck!