software-mansion / react-native-reanimated

React Native's Animated library reimplemented
https://docs.swmansion.com/react-native-reanimated/
MIT License
8.87k stars 1.3k forks source link

added warning when animated component is rendered directly inside SafeAreaView #6052

Closed exploIF closed 3 months ago

exploIF commented 4 months ago

Summary

Added warning log when animated component is rendered directly inside SafeAreaView from react-native or from react-native-safe-area-context. This should address maintainer issue here

Test plan

  1. Inside EmptyExample.tsx paste the code
    
    import { StyleSheet, View, SafeAreaView as RNSafeAreaView } from 'react-native';
    import React from 'react';
    import Animated, { FadeIn } from 'react-native-reanimated';

const animation = FadeIn.delay(1000).duration(2000);

export default function EmptyExample() { return (

); } const styles = StyleSheet.create({ greyBox: { backgroundColor: 'grey', height: 100, width: '100%', }, redAnimatedBox: { backgroundColor: 'red', height: 100, width: '100%', }, });

2. This should throw a warning in your console:

Animated components shouldn't be rendered directly inside , consider wrapping your content with additional


3. Apply changes below
```diff
import { StyleSheet, View, SafeAreaView as RNSafeAreaView } from 'react-native';
import React from 'react';
import Animated, { FadeIn } from 'react-native-reanimated';

const animation = FadeIn.delay(1000).duration(2000);

export default function EmptyExample() {
  return (
    <RNSafeAreaView>
+      <View>
        <View style={styles.greyBox} />
        <Animated.View entering={animation}>
          <View style={styles.redAnimatedBox} />
        </Animated.View>
+      </View>
    </RNSafeAreaView>
  );
}
const styles = StyleSheet.create({
  greyBox: {
    backgroundColor: 'grey',
    height: 100,
    width: '100%',
  },
  redAnimatedBox: {
    backgroundColor: 'red',
    height: 100,
    width: '100%',
  },
});
  1. Reload the app, there shouldn't be a warning message in the console.
piaskowyk commented 3 months ago

As we discussed offline, this issue is more generic than just the SafeAreaContext. We need to modify the implementation to cancel the animation and jump directly to the final position. A similar approach is already in place for Layout Animation for Fabric. Solutions: