Skip to content

Instantly share code, notes, and snippets.

@aleclarson
Created November 2, 2019 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleclarson/140cf04302b2df5c60233065c57d8dd7 to your computer and use it in GitHub Desktop.
Save aleclarson/140cf04302b2df5c60233065c57d8dd7 to your computer and use it in GitHub Desktop.
React controlled unmount
import React from 'react'
import { animated, useSpring } from 'react-spring'
const Example = () => {
const [unmount, isUnmounting] = React.useUnmount()
const { opacity } = useSpring({
// Fade in "opacity" from 0
from: { opacity: 0 },
// Fade out "opacity" to 0 when ready to unmount
opacity: isUnmounting ? 0 : 1,
// Commit delayed unmount when fade out ends
onRest: unmount,
})
return <animated.div style={{ opacity, width: 100, height: 100, background: 'red' }} />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment