React-native-reanimated: (measure) method cannot be used on RN side!

Created on 31 Aug 2020  路  10Comments  路  Source: software-mansion/react-native-reanimated

Description

When I use the measure function like that :

  const style = useAnimatedStyle(() => {
    const measured = measure(aref);

    return {
      width: measured.width,
      height: measured.height,
    };
  });

I get the error message :
(measure) method cannot be used on RN side!

Screenshots

Steps To Reproduce

1.

Expected behavior

Actual behavior

I'd like for this measure to work

Snack or minimal code example

Package versions

  • React:
  • React Native:
  • React Native Reanimated: alpha 5
馃彔 Reanimated2 馃悶 Bug

Most helpful comment

having the same issue.

All 10 comments

@Daavidaviid Could you provide full component code?

Sure. Here it is :

interface Props {}
export const NotifPlayground = memo(({}: Props) => {
  const oscillation = useSharedValue(1);
  const aRef = useAnimatedRef();

  useEffect(() => {
    oscillation.value = repeat(withTiming(10, { duration: 1000 }), -1, true);
  }, [oscillation.value]);

  useDerivedValue(() => {
    const width = measure(aRef).width;
    console.log({ width });
  });

  const style = useAnimatedStyle(() => {
    return {
      borderWidth: oscillation.value,
    };
  });

  return (
    <Animated.View ref={aRef} style={styles.container}>
      <Animated.View style={styles.cc}>
        <Animated.View style={[styles.circle, style]} />
      </Animated.View>
    </Animated.View>
  );
});

And the funny thing is, if I comment the content of the useDerivedValue callback, and relaunch the app, when I hot reload after I uncomment, it works. But the launching it without the comments it crashes with (measure) method cannot be used on RN side!

having the same issue.

Unfortunately, there is no sync measure on the RN side right now. useAnimatedStyle is called on RN side to get the initial state. I would suggest to wrap it in try catch for now.

But most probably for the initial call in useAnimatedStyle it will return null for all the measurements anyway once we "fix" it.

Also, could you provide real life example so we could help to workaround it at least for you because current usage doesn't make any sense :)

@terrysahaidak this doc example doesn't work and raises the above error.

```
const Comp = () => {
const aref = useAnimatedRef();

useDerivedValue(() => {
const measured = measure(aref);
// ...
});

return ;
};
``

Will it be ok if the initial value was null? Because in order to have "initial styles" for view which you render and then measure, we need to run everything on the RN side first.

@terrysahaidak hmm, so when do u suggest is appropriate to measure a certain view ?

let's say I'm doing some sort of an accordion & I wanna measure the children height to be able to toggle it on / off ( items are closed by default ).

when would I measure the height ?

  • Item

    • child 1

    • child 2

  • Item 2

    • child 1

    • child 2

    • child 3

  • Item 3

    • child 1

    • child 2

    • child 3

Check out the measure example. It's doing exactly this.The measure function is being executed in the tap handler.

will do thank you @terrysahaidak

Was this page helpful?
0 / 5 - 0 ratings