-
-
Notifications
You must be signed in to change notification settings - Fork 514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: screen rewritten as functional component #2111
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ScreenProps> = | ||
ScreenNativeComponent as any; | ||
ScreenNativeComponent as React.ComponentType<ScreenProps>; | ||
const AnimatedNativeScreen = Animated.createAnimatedComponent(NativeScreen); | ||
const AnimatedNativeModalScreen = Animated.createAnimatedComponent( | ||
ModalScreenNativeComponent as React.ComponentType<ScreenProps> | ||
|
@@ -42,27 +41,25 @@ interface ViewConfig extends View { | |
}; | ||
} | ||
|
||
export class InnerScreen extends React.Component<ScreenProps> { | ||
private ref: React.ElementRef<typeof View> | 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<View, ScreenProps>( | ||
function InnerScreen(props, ref) { | ||
const innerRef = React.useRef<ViewConfig | null>(null); | ||
React.useImperativeHandle(ref, () => innerRef.current!, []); | ||
|
||
setNativeProps(props: ScreenProps): void { | ||
this.ref?.setNativeProps(props); | ||
} | ||
const setRef = (ref: ViewConfig) => { | ||
Comment on lines
-51
to
-53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before we can remove this method as unused, we need to make sure no one actually uses it 😅 We need to:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is this intentional & you are utilising this method defined on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe |
||
innerRef.current = ref; | ||
props.onComponentRef?.(ref); | ||
}; | ||
|
||
setRef = (ref: React.ElementRef<typeof View> | null): void => { | ||
this.ref = ref; | ||
this.props.onComponentRef?.(ref); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to see some comment why do we resign from calling this callback here (onComponentRef) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's an oversight, brought it back |
||
const closing = new Animated.Value(0); | ||
const progress = new Animated.Value(0); | ||
const goingForward = new Animated.Value(0); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are moved from state to being recreated on every rerender. Is that intentional? Do we expect any issues here? @WoLewicki asking for some insights here. I'm not really familiar with semantics of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe leveraging the useRef hook to store the values would be a good idea? What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was also my initial thought & I think we should go for this. In any case I would like to hear from someone who might now some internals (already tagged). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I can see that in the example they use |
||
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<ScreenProps> { | |
...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<ScreenProps> { | |
[ | ||
{ | ||
nativeEvent: { | ||
progress: this.progress, | ||
closing: this.closing, | ||
goingForward: this.goingForward, | ||
progress, | ||
closing, | ||
goingForward, | ||
}, | ||
}, | ||
], | ||
|
@@ -168,9 +165,9 @@ export class InnerScreen extends React.Component<ScreenProps> { | |
) : ( | ||
<TransitionProgressContext.Provider | ||
value={{ | ||
progress: this.progress, | ||
closing: this.closing, | ||
goingForward: this.goingForward, | ||
progress, | ||
closing, | ||
goingForward, | ||
}}> | ||
{children} | ||
</TransitionProgressContext.Provider> | ||
|
@@ -195,25 +192,22 @@ export class InnerScreen extends React.Component<ScreenProps> { | |
return ( | ||
<Animated.View | ||
style={[style, { display: activityState !== 0 ? 'flex' : 'none' }]} | ||
ref={this.setRef} | ||
ref={setRef} | ||
{...props} | ||
/> | ||
); | ||
} | ||
} | ||
} | ||
); | ||
|
||
// 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<ScreenProps> { | ||
static contextType = ScreenContext; | ||
const Screen: React.FC<ScreenProps> = props => { | ||
const ScreenWrapper = React.useContext(ScreenContext) || InnerScreen; | ||
|
||
render() { | ||
const ScreenWrapper = (this.context || InnerScreen) as React.ElementType; | ||
return <ScreenWrapper {...this.props} />; | ||
} | ||
} | ||
return <ScreenWrapper {...props} />; | ||
}; | ||
|
||
export default Screen; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this line for?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is intended to replace removed code mentioned above:
setNativeProps(props: ScreenProps): void { this.ref?.setNativeProps(props); }
However I am not sure wether it is correctly implemented