0

I am trying to get the measurement of the tabs.

Here is the data for this test app. I am creating ref here

const images = {
  man: 'https://www.gogole.com',
  ....
};

const data = Object.keys(images).map(i => ({
  key: i,
  title: i,
  image: images[i],
  ref: React.createRef(),
}));

Now I am trying to get the measurement of each tab like this

const Tab = React.forwardRef(({item}, ref) => {
  return (
    <View ref={ref} key={item.title}>
      <Text>{item.title}</Text>
    </View>
  );
});

const Tabs = ({data, scrollX}) => {
  const containerRef = useRef({});
  React.useEffect(() => {
    data && data.forEach((item) => {
      item.ref.current.measureLayout(
        containerRef.current,
        (x, y, width, height) => {
          console.log(x, y, width, height);
        },
      );
    });
  },[]);
  return (
    <View
      style={{
        position: 'absolute',
      }}>
      {data.map((data, index) => (
        <Tab key={index} item={data} ref={data.ref} />
      ))}
    </View>
  );
};

return (
    <View style={styles.container}>
      <Tabs data={data} />
</View>
)

As you can see I am trying to console.log the measurement in the Tabs. But the app doesn't return the measurement but always this line

Warning: ref.measureLayout must be called with a node handle or a ref to a native component.

From my research in other GitHub treads , there is something called relativeToNativeNode which is deprecated now. Is my error related to this?

So, to avoid the error and get the measurement. What should I change in the code?

Kerry
  • 384
  • 3
  • 25
  • I have no idea but maybe this helps https://gist.github.com/phuochau/af26c22b2b69bf1f7a3a389912d8dbd3 – RST Jul 20 '21 at 11:26
  • @Kerry I think it's due to how ref you are storing instead of `data.ref` create ref array and store inside that. Refer this https://stackoverflow.com/a/56063129/7561290 – Javascript Hupp Technologies Jul 20 '21 at 12:05

0 Answers0