diff --git a/src/components/Screen.tsx b/src/components/Screen.tsx index 45767f74bc..997d527437 100644 --- a/src/components/Screen.tsx +++ b/src/components/Screen.tsx @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ import React from 'react'; import { Animated, View, Platform } from 'react-native'; @@ -17,7 +16,7 @@ import ScreenNativeComponent from '../fabric/ScreenNativeComponent'; import ModalScreenNativeComponent from '../fabric/ModalScreenNativeComponent'; export const NativeScreen: React.ComponentType = - ScreenNativeComponent as any; + ScreenNativeComponent as React.ComponentType; const AnimatedNativeScreen = Animated.createAnimatedComponent(NativeScreen); const AnimatedNativeModalScreen = Animated.createAnimatedComponent( ModalScreenNativeComponent as React.ComponentType @@ -42,27 +41,25 @@ interface ViewConfig extends View { }; } -export class InnerScreen extends React.Component { - private ref: React.ElementRef | null = null; - private closing = new Animated.Value(0); - private progress = new Animated.Value(0); - private goingForward = new Animated.Value(0); +export const InnerScreen = React.forwardRef( + function InnerScreen(props, ref) { + const innerRef = React.useRef(null); + React.useImperativeHandle(ref, () => innerRef.current!, []); - setNativeProps(props: ScreenProps): void { - this.ref?.setNativeProps(props); - } + const setRef = (ref: ViewConfig) => { + innerRef.current = ref; + props.onComponentRef?.(ref); + }; - setRef = (ref: React.ElementRef | null): void => { - this.ref = ref; - this.props.onComponentRef?.(ref); - }; + const closing = React.useRef(new Animated.Value(0)).current; + const progress = React.useRef(new Animated.Value(0)).current; + const goingForward = React.useRef(new Animated.Value(0)).current; - render() { const { enabled = screensEnabled(), freezeOnBlur = freezeEnabled(), ...rest - } = this.props; + } = props; // To maintain default behavior of formSheet stack presentation style and to have reasonable // defaults for new medium-detent iOS API we need to set defaults here @@ -112,13 +109,13 @@ export class InnerScreen extends React.Component { ...ref.viewConfig.validAttributes.style, display: false, }; - this.setRef(ref); + setRef(ref); } else if (ref?._viewConfig?.validAttributes?.style) { ref._viewConfig.validAttributes.style = { ...ref._viewConfig.validAttributes.style, display: false, }; - this.setRef(ref); + setRef(ref); } }; @@ -148,9 +145,9 @@ export class InnerScreen extends React.Component { [ { nativeEvent: { - progress: this.progress, - closing: this.closing, - goingForward: this.goingForward, + progress, + closing, + goingForward, }, }, ], @@ -168,9 +165,9 @@ export class InnerScreen extends React.Component { ) : ( {children} @@ -195,25 +192,22 @@ export class InnerScreen extends React.Component { return ( ); } } -} +); // context to be used when the user wants to use enhanced implementation // e.g. to use `useReanimatedTransitionProgress` (see `reanimated` folder in repo) export const ScreenContext = React.createContext(InnerScreen); -class Screen extends React.Component { - static contextType = ScreenContext; +const Screen: React.FC = props => { + const ScreenWrapper = React.useContext(ScreenContext) || InnerScreen; - render() { - const ScreenWrapper = (this.context || InnerScreen) as React.ElementType; - return ; - } -} + return ; +}; export default Screen;