productbrew / react-native-reanimated-flip

35 stars 12 forks source link

TypeError on usage #51

Open MauriceNino opened 2 years ago

MauriceNino commented 2 years ago

Reanimated is configured properly and already used in other parts of the app.

The error I am getting is the following:

TypeError: _reactNativeReanimated.default.interpolate is not a function. (In '_reactNativeReanimated.default.interpolate(side, [0, 1], [180, 360])', '_reactNativeReanimated.default.interpolate' is undefined)

This error is located at:
    in ReanimatedFlip (at memory.tsx:19)
    in MemoryImage (at memory.tsx:62)
    in RCTView (at View.js:32)
Package  Version
react-native 0.67.3
react-native-flip 0.2.2
react-native-reanimated 2.9.1
czystyl commented 2 years ago

Hey @MauriceNino, I'll try to find some time to check it out.

MauriceNino commented 2 years ago

Thanks a lot!

buryo commented 2 years ago

I guess this package doesn't work anymore, I get the same error.

felipecorbee commented 1 year ago

Same issue here

finnp commented 11 months ago

Here's a fixed version for react-native-reanimated 3.6.0. I also created a PR for this: https://github.com/productbrew/react-native-reanimated-flip/pull/58

import Animated, {
  Easing,
  interpolate,
  useAnimatedStyle,
  useDerivedValue,
  withTiming,
} from 'react-native-reanimated';
import {StyleSheet, ViewStyle} from 'react-native';
import React from 'react';

export enum FlipSide {
  FRONT,
  BACK,
}

export enum RotateAxis {
  Y = 'Y',
  X = 'X',
}

type Props = {
  perspective?: number;
  side: FlipSide;
  rotate?: RotateAxis;
  style?: ViewStyle;
  front: React.ReactElement;
  back: React.ReactElement;
};

const ReanimatedFlip = ({
  perspective = 1200,
  rotate = RotateAxis.Y,
  side,
  front,
  back,
  style,
}: Props) => {
  const rotatePosition = interpolate(side, [0, 1], [180, 360]);

  const rotateValue = useDerivedValue(() => {
    return withTiming(rotatePosition, {
      duration: 500,
      easing: Easing.inOut(Easing.ease),
    });
  });

  const rotationFlip = useDerivedValue(() => {
    if (rotate === RotateAxis.Y) {
      return {
        rotateY: `${rotateValue.value}deg`,
      };
    }

    return {
      rotateX: `${rotateValue.value}deg`,
    };
  }, [rotate, rotateValue]);

  const rotationFlipBack = useDerivedValue(() => {
    if (rotate === RotateAxis.Y) {
      return {
        rotateY: '180deg',
      };
    }

    return {
      rotateX: '180deg',
    };
  }, [rotate]);

  const opacityFront = useDerivedValue(() => {
    return withTiming(side, {
      duration: 500,
      easing: Easing.inOut(Easing.ease),
    });
  }, [side]);

  const opacityBack = useDerivedValue(() => {
    return withTiming(side === 0 ? 1 : 0, {
      duration: 500,
      easing: Easing.inOut(Easing.ease),
    });
  }, [side]);

  const animatedStyleFront = useAnimatedStyle(() => {
    return {
      opacity: opacityFront.value,
      transform: [{perspective}, {...rotationFlip.value}],
    };
  }, [rotate, side, rotationFlip]);

  const animatedStyleBack = useAnimatedStyle(() => {
    return {
      opacity: opacityBack.value,
      transform: [
        {perspective},
        {...rotationFlipBack.value},
        {...rotationFlip.value},
      ],
    };
  }, [rotate, side]);

  return (
    <Animated.View style={StyleSheet.flatten([style, styles.container])}>
      <Animated.View style={[styles.side, animatedStyleFront]}>
        {front}
      </Animated.View>
      <Animated.View style={[styles.side, animatedStyleBack]}>
        {back}
      </Animated.View>
    </Animated.View>
  );
};

const styles = StyleSheet.create({
  container: {
    justifyContent: 'center',
    alignItems: 'center',
    height: '100%',
    width: '100%'
  },
  side: {
    width: '100%',
    height: '100%',
    position: 'absolute',
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default ReanimatedFlip;