import React, { Ref, useImperativeHandle, useRef, useState } from 'react'; import { Animated, Easing, StyleProp, ViewStyle } from 'react-native'; export type RefActions = { displayComponent: () => void; dismissComponent: () => void; }; function AnimationedPopUp( props: { children: any; style?: StyleProp }, ref: Ref ) { const opaqueAnimationValue = useRef( new Animated.Value(0) ).current; const opaquePlusAnimationRef = useRef( Animated.timing(opaqueAnimationValue, { toValue: 1, duration: 300, easing: Easing.linear, useNativeDriver: true, }) ).current; const opaqueReduceAnimationRef = useRef( Animated.timing(opaqueAnimationValue, { toValue: 0, duration: 300, easing: Easing.linear, useNativeDriver: true, }) ).current; const [display, setDisplay] = useState(false); function displayComponent() { setDisplay(true); opaquePlusAnimationRef.start(); } function dismissComponent() { opaqueReduceAnimationRef.start(); setTimeout(() => { setDisplay(false); }); } useImperativeHandle( // Parameter 1: the ref that is exposed to the parent ref, // Parameter 2: a function that returns the value of the ref's current property, // an object containing the things we're trying to expose (in this case, just // one function) () => { return { displayComponent: displayComponent, dismissComponent: dismissComponent, }; } ); return display ? ( {props.children} ) : null; } export const RefedAnimationPopUp = React.forwardRef(AnimationedPopUp);