Last active
January 8, 2024 10:29
-
-
Save DimitryDushkin/2a5db8d854288af224b9190be4a9452e to your computer and use it in GitHub Desktop.
React Native animated fade-in and fade-out
This file contains 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 {StyleProp, ViewStyle} from 'react-native'; | |
import * as Animatable from 'react-native-animatable'; | |
type Props = { | |
isVisible: boolean, | |
children: React.ReactNode, | |
style?: StyleProp<ViewStyle>, | |
}; | |
export default function AnimatableFade({isVisible, children, style}: Props) { | |
return ( | |
<Animatable.View | |
transition={['opacity']} | |
style={[ | |
{opacity: isVisible ? 1 : 0}, | |
style, | |
]} | |
useNativeDriver | |
> | |
{children} | |
</Animatable.View> | |
); | |
} | |
// How to use | |
function App() { | |
const [isVisible, setVisibility] = useState(false); | |
return ( | |
<> | |
<Button onPress={() => setVisibility(!isVisible)}>Toggle</Button> | |
<AnimatedFade isVisible={isVisible}><Text>HEY LOOK!</Text></AnimatedFade> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment