Skip to content

Instantly share code, notes, and snippets.

@tobob
Created December 18, 2019 10:53
Show Gist options
  • Save tobob/3a4e399bf9ba0f4ca819ee02039265d5 to your computer and use it in GitHub Desktop.
Save tobob/3a4e399bf9ba0f4ca819ee02039265d5 to your computer and use it in GitHub Desktop.
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