I have an animation that works great moving items in from the right and off to the left. I want to do the opposite when the back button is clicked, similar to popping a View off the navigation stack in a mobile application. My code is below, how can I get the animation to run backwards when the button is clicked? I am using React 17.0.2 and react-spring 9.2.3 .
import React, { useState, useCallback, CSSProperties, useEffect } from "react";
import {
useTransition,
animated,
AnimatedProps,
useSpringRef,
} from "react-spring";
import styles from "../styles/components/carousel.module.css";
const Carousel: React.FC = () => {
const [index, set] = useState(0);
const onClick = useCallback(() => set((state) => (state + 1) % 6), []);
const transRef = useSpringRef();
const transitions = useTransition(index, {
ref: transRef,
keys: null,
from: { opacity: 0, transform: "translate3d(100%,0,0)" },
enter: { opacity: 1, transform: "translate3d(0%,0,0)" },
leave: { opacity: 0, transform: "translate3d(-50%,0,0)" },
});
useEffect(() => {
transRef.start();
}, [index, transRef]);
const pageContent: { text: string; color: string }[] = [
{ text: "one", color: "lightpink" },
{ text: "two", color: "lightblue" },
{ text: "three", color: "lightgreen" },
];
const pages: ((
props: AnimatedProps<{ style: CSSProperties }>
) => React.ReactElement)[] = pageContent.map((obj) => {
return function createAnimatedDiv({ style }) {
return (
<animated.div style={{ ...style, background: obj.color }}>
{obj.text}
</animated.div>
);
};
});
const onBackClicked = useCallback(() => set((state) => (state - 1) % 6), []);
return (
<>
<button onClick={onBackClicked}>Back</button>
<div className={`flex fill ${styles.container}`} onClick={onClick}>
{transitions((style, i) => {
const Page = pages[i];
return <Page style={style} />;
})}
</div>
</>
);
};
export default Carousel;