React Native: Error while updating property 'transform' of a view managed by: RCTView in Android

I am developing some application with React Native. First, I was checking Android and IOS at the same time. After that, I made a mistake. I continued with only IOS. After a while, I had run in Android Emulator then I saw the app is crashed (Error is attached). I doubt the animations. I will not add all the codes of the project to not waste anyone's time. I have added the codes of animated CardFlip. It gives the error when only I opened the page which has the animated component. I hope that somebody might help me with this.

enter image description here

CardFlip Component

import React, { ReactNode, useEffect, useState } from 'react';
import { Animated, StyleSheet, ViewStyle, View, TouchableWithoutFeedback } from 'react-native';
import { isAndroid } from 'Utils/Device';
import { Helpers } from 'Theme/index';

interface CardFlipProps {
    condition?: boolean
    children: [ReactNode, ReactNode]
    containerStyle?: ViewStyle|Array<ViewStyle>
}

const CardFlip: React.FunctionComponent<CardFlipProps> = ({ children, condition = true, containerStyle = {} }): JSX.Element => {
    const [flip, setFlip] = useState(false);
    const [rotate] = useState(new Animated.Value(0));

    const startAnimation = ({ toRotate }) => {
        Animated.parallel([
            Animated.spring(rotate, {
                toValue: toRotate,
                friction: 8,
                tension: 1,
            }),
        ]).start();
    };

    useEffect(() => {
        if (flip) {
            startAnimation({ toRotate: 1 });
        } else {
            startAnimation({ toRotate: 0 });
        }
    }, [flip]);

    useEffect(() => {
        if (!condition) {
            setFlip(false);
        }
    }, [condition]);

    const interpolatedFrontRotate = rotate.interpolate({
        inputRange: [0, 1],
        outputRange: ['0deg', '180deg'],
    });

    const interpolatedBackRotate = rotate.interpolate({
        inputRange: [0, 1],
        outputRange: ['180deg', '0deg'],
    });

    const frontOpacity = rotate.interpolate({
        inputRange: [0, 1],
        outputRange: [1, 0.7],
    });

    const backOpacity = rotate.interpolate({
        inputRange: [0, 1],
        outputRange: [0.7, 1],
    });

    const perspective = isAndroid() ? undefined : 1000;

    return (
        <TouchableWithoutFeedback onPress={ () => condition && setFlip(!flip) }>
            <View style={ containerStyle }>
                <Animated.View style={ [
                    style.card,
                    { transform: [{ rotateY: interpolatedFrontRotate }, { perspective }] },
                    { opacity: frontOpacity },
                ] }>
                    { children[0] }
                </Animated.View>
                <Animated.View style={ [
                    style.card,
                    { transform: [{ rotateY: interpolatedBackRotate }, { perspective }] },
                    { opacity: backOpacity },
                ] }>
                    { children[1] }
                </Animated.View>
            </View>
        </TouchableWithoutFeedback>
    );
};

interface Styles {
    card: ViewStyle
}

const style = StyleSheet.create<Styles>({
    card: {
        ...StyleSheet.absoluteFillObject,
        width: '100%',
        height: '100%',
        backfaceVisibility: 'hidden',
        ...Helpers.center,
    },
});

export default CardFlip;

Upvotes: 0

Views: 8899

Answers (2)

sebastianf182
sebastianf182

Reputation: 9978

Just moving my comment as answer as requested. It seems that the perspective value could be null or a bug in Android so either removing it or using it conditionally should work.

Upvotes: 3

Dogu Deniz Ugur
Dogu Deniz Ugur

Reputation: 329

in the lines { transform: [{ rotateY: interpolatedFrontRotate }, { perspective }] }, and { transform: [{ rotateY: interpolatedBackRotate }, { perspective }] }, : removing perspective or implementing it as { transform: [{ rotateY: interpolatedFrontRotate }, { perspective: perspective }] }, may solve your problem. In addition, you can rename the const perspective to avoid confusion.

Upvotes: 1

Related Questions