Created
December 18, 2019 10:53
-
-
Save tobob/3a4e399bf9ba0f4ca819ee02039265d5 to your computer and use it in GitHub Desktop.
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, {useContext, useEffect, useRef} from 'react'; | |
import {ToastContext} from './ToastContext'; | |
import { | |
Text, | |
Animated, | |
Easing, | |
TouchableOpacity, | |
StyleSheet, | |
} from 'react-native'; | |
export const Toast = () => { | |
const {toast, hide} = useContext(ToastContext); | |
const translateYRef = useRef(new Animated.Value(-100)); | |
useEffect(() => { | |
if (toast.visible) { | |
Animated.timing(translateYRef.current, { | |
duration: 300, | |
easing: Easing.ease, | |
toValue: 100, | |
useNativeDriver: true, | |
}).start(); | |
} else { | |
Animated.timing(translateYRef.current, { | |
duration: 450, | |
easing: Easing.ease, | |
toValue: -100, | |
useNativeDriver: true, | |
}).start(); | |
} | |
}, [toast]); | |
return ( | |
<Animated.View | |
style={[ | |
styles.toast, | |
{transform: [{translateY: translateYRef.current}]}, | |
]}> | |
<TouchableOpacity onPress={hide} style={styles.content}> | |
<Text style={styles.toastMessage}> {toast.message}</Text> | |
</TouchableOpacity> | |
</Animated.View> | |
); | |
}; | |
export default Toast; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment