Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warning about overwriting style with layout animation #5644

Merged
merged 7 commits into from
Feb 20, 2024

Conversation

Latropos
Copy link
Contributor

@Latropos Latropos commented Feb 6, 2024

Summary

In this PR #4969 I didn't include neither animatedStyles nor styles nested in arrays.
That's why the problem wasn't reported in this example: #5395

closes #5395

Before After
image image

Test plan

Code
import Animated, {
  useAnimatedStyle,
  SlideInDown,
  ZoomOut,
  useDerivedValue,
  withSpring,
  interpolate,
} from 'react-native-reanimated';
import { StyleSheet, View } from 'react-native';

import React, { useEffect, useState } from 'react';

interface Item {
  color: string;
}

function getRandomColor(): string {
  return '#' + Math.floor(Math.random() * 16777215).toString(16);
}

const Entering = SlideInDown.springify().mass(1).stiffness(1000).damping(500);
const Exiting = ZoomOut.springify().mass(1).stiffness(1000).damping(500);

function ItemComponent({ item, index }: { item: Item; index: number }) {
  const rotation = useDerivedValue(() => withSpring(index * 15), [index]);

  const animatedStyle = useAnimatedStyle(() => {
    return {
      zIndex: -index,
      backgroundColor: item.color,
      transform: [
        {
          translateX: interpolate(rotation.value, [0, 100], [0, 100]),
        },
        {
          translateY: interpolate(rotation.value, [0, 100], [0, -30]),
        },
        {
          scale: interpolate(rotation.value, [0, 100], [1, 0.7]),
        },
        {
          rotate: `${rotation.value}deg`,
        },
      ],
    };
  }, [rotation, index, item]);

  return (
    <Animated.View
      entering={Entering}
      exiting={Exiting}
      style={[styles.item, animatedStyle]}
    />
  );
}

export default function StackedLayoutAnimationExample() {
  const [items, setItems] = useState<Item[]>([
    { color: 'red' },
    { color: 'green' },
  ]);

  useEffect(() => {
    const interval = setInterval(() => {
      setItems((i) => [{ color: getRandomColor() }, ...i]);
    }, 1500);
    return () => clearInterval(interval);
  }, []);

  return (
    <View style={styles.container}>
      {items.map((item, index) => {
        if (index > 2) {
          return null;
        }
        return <ItemComponent key={item.color} item={item} index={index} />;
      })}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  item: {
    width: 100,
    height: 150,
    borderRadius: 15,
    position: 'absolute',
  },
});

@Latropos Latropos marked this pull request as ready for review February 6, 2024 11:02
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Show resolved Hide resolved
Co-authored-by: Tomek Zawadzki <tomasz.zawadzki@swmansion.com>
Copy link
Member

@tomekzaw tomekzaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this warning!

Copy link
Collaborator

@tjzel tjzel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good, names of some variables could be better but it's very minor. Left a suggestion if you are interested in it, but your solution is good as well.

TS types are not the best, but since this file hasn't undergone proper typing it's okay.

src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
src/animationBuilder.tsx Outdated Show resolved Hide resolved
@Latropos Latropos added this pull request to the merge queue Feb 20, 2024
Merged via the queue into main with commit 896be21 Feb 20, 2024
7 checks passed
@Latropos Latropos deleted the acynk/fix-error-overwrtie-style branch February 20, 2024 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LayoutAnimations entering and exiting not working
3 participants