Coder Social home page Coder Social logo

Comments (3)

exploIF avatar exploIF commented on June 15, 2024 1

@Julioevm Hi there, it looks to me like your CustomExitingAnimation is not working because your shared value never gets updated (it's using initial value all the time, so value from the very first render). Also there is no need to store animation directions inside state, you can store it directly as a shared value, pass it to the children component and then use useDerivedValue in order to access it.

I've modified your example to achieve proper exiting animation. Please check below

import { Text, StyleSheet, View, SafeAreaView, Button } from 'react-native';

import React, { useState } from 'react';

import Animated, {
  SlideOutRight,
  SlideOutLeft,
  useSharedValue,
  useDerivedValue,
  ExitAnimationsValues,
  SharedValue,
} from 'react-native-reanimated';

const slideOutLeftAnimation = new SlideOutLeft().build();
const slideOutRightAnimation = new SlideOutRight().build();

function Card({ animation }: { animation: Readonly<SharedValue> }) {
  const animationDirection = useDerivedValue(() => animation.value);

  const CustomExitingAnimation = (values: ExitAnimationsValues) => {
    'worklet';

    return animationDirection.value === 'right'
      ? slideOutLeftAnimation(values)
      : slideOutRightAnimation(values);
  };

  return (
    <Animated.View exiting={CustomExitingAnimation}>
      <View style={styles.cardContainer}>
        <Text style={styles.paragraph}>Let's go</Text>
      </View>
    </Animated.View>
  );
}

export default function App() {
  const [number, setNumber] = useState(0);

  const animation = useSharedValue('right');

  const nextButton = () => {
    animation.value = 'right';
    setNumber((n) => n + 1);
  };
  const prevButton = () => {
    animation.value = 'left';
    setNumber((n) => n - 1);
  };

  return (
    <SafeAreaView style={styles.container}>
      <Card key={number} animation={animation} />
      <View style={styles.buttonWrapper}>
        <Button onPress={prevButton} title={'prev'} />
        <Button onPress={nextButton} title={'next'} />
      </View>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  paragraph: {
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    textAlign: 'center',
  },
  buttonWrapper: {
    flexDirection: 'row',
  },
  cardContainer: {
    alignItems: 'center',
    justifyContent: 'center',
    padding: 24,
    backgroundColor: 'aquamarine',
    margin: 20,
  },
});

from react-native-reanimated.

hanwenbo avatar hanwenbo commented on June 15, 2024

+1

from react-native-reanimated.

Julioevm avatar Julioevm commented on June 15, 2024

Thanks a lot @exploIF!

from react-native-reanimated.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.