Skip to content
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

Merged
merged 3 commits into from
May 6, 2024

Conversation

alduzy
Copy link
Member

@alduzy alduzy commented Apr 25, 2024

Description

This PR intents to change Screen component into a functional component.

@alduzy alduzy requested review from tboba and WoLewicki April 25, 2024 15:23
@alduzy alduzy changed the title refactor: screen rewritten as component refactor: screen rewritten as functional component Apr 25, 2024
@WoLewicki WoLewicki requested a review from kkafar April 25, 2024 15:27
Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, great job 🎉 I've got few remarks and questions we need to answer before we can merge these changes.

The rest of changes look very promising, but I would ask you to test them within our TestsExample application in following scenarios:

  1. using native-stack v5 (createNativeStackNavigator imported from react-native-screens
  2. using native-stack v6 (createNativeStackNavigator imported from @react-navigation/native-stack

Please be aware of prop naming differences between v5 & v6.

We need to check few selected tests (1072, 1069 and some with reanimated should be amongst them) whether we don't have any undesired behaviour in downstream projects.

Comment on lines 53 to 55
const closing = new Animated.Value(0);
const progress = new Animated.Value(0);
const goingForward = new Animated.Value(0);
Copy link
Member

Choose a reason for hiding this comment

The 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 Animated values.

Copy link
Member Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I can see that in the example they use useRef for it: https://reactnative.dev/docs/animated#example. If it works correctly then it seems like the way to go. I can also see that a similar concept is used in react-navigation: https://github.com/react-navigation/react-navigation/blob/d0abdee67f5db8cf39112af535846ffededfb21d/packages/react-native-tab-view/src/useAnimatedValue.tsx#L4

Comment on lines -51 to -53
setNativeProps(props: ScreenProps): void {
this.ref?.setNativeProps(props);
}
Copy link
Member

Choose a reason for hiding this comment

The 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:

  1. check react-navigation code for any usages of these,
  2. as this seems like method that could be utilised by react-native-reanimated - we need to also look for usages there (also see our ReanimatedScreenProvider) - it would be best to consult with reanimated team,
  3. thinking now, I would rather leave this method exposed in this PR 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is this intentional & you are utilising this method defined on innerRef?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Animated still uses setNativeProps under the hood so I'd leave it. After quick talk with @piaskowyk, react-native-reanimated is not utilizing it, but still we would want it for Animated. @alduzy did you maybe check examples with Animated to see if they work when this code is removed? Would be also good to check with both useNativeDriver set to false and true and in TestsExample and FabricTestsExample.


setRef = (ref: React.ElementRef<typeof View> | null): void => {
this.ref = ref;
this.props.onComponentRef?.(ref);
Copy link
Member

@kkafar kkafar Apr 26, 2024

Choose a reason for hiding this comment

The 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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an oversight, brought it back

@kkafar kkafar self-requested a review April 26, 2024 09:05
Copy link
Member

@WoLewicki WoLewicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I answered some questions and left my own. Please answer them. Good job overall 🎉

export const InnerScreen = React.forwardRef<View, ScreenProps>(
function InnerScreen(props, ref) {
const innerRef = React.useRef<ViewConfig | null>(null);
React.useImperativeHandle(ref, () => innerRef.current!, []);
Copy link
Member

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?

Copy link
Member Author

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

@alduzy alduzy requested a review from WoLewicki April 29, 2024 06:38
Copy link
Member

@WoLewicki WoLewicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! If it has been tested with both react-native-reanimated and Animated and on both new and old architecture, then we can merge it 🚀

Copy link
Member

@kkafar kkafar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the above comment from @WoLewicki

Copy link
Member

@tboba tboba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 🥳

@alduzy alduzy merged commit 6274fb7 into main May 6, 2024
3 checks passed
@alduzy alduzy deleted the @alduzy/rewrite-screen-to-component branch May 6, 2024 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants