Created
November 2, 2019 12:49
-
-
Save aleclarson/140cf04302b2df5c60233065c57d8dd7 to your computer and use it in GitHub Desktop.
React controlled unmount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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