React Animate Leaving Elements #147
-
|
I guess So this doesn't work (as expected even though its called react doesn't wait for animation to finish before removing the element) Is there any way to achieve this? (Im trying to animate elements as they enter and leave a list) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Yeah, this is actually possible with <AnimatePresence>
{isVisible && <MyComponent />}
</AnimatePresence>function MyComponent() {
const ref= useRef(null)
const [isPresent, safeToRemove] = usePresence()
useEffect(() => {
animate(ref.current, { opacity: isPresent ? 1 : 0 }).finished.then(safeToRemove)
}, [isPresent])
return <div ref={ref} />
} |
Beta Was this translation helpful? Give feedback.
-
|
You don’t need LazyMotion, that snippet is all you need for AnimatePresence with Motion One. But for layout animations you would need to use Framer Motion’s |
Beta Was this translation helpful? Give feedback.
Yeah, this is actually possible with
AnimatePresenceitself and the usePresence hook.