-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
[CP Stag] Only use tab animation when in tab navigator #27974
Changes from 1 commit
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 |
---|---|---|
|
@@ -19,12 +19,19 @@ function NavigationAwareCamera({cameraTabIndex, forwardedRef, ...props}) { | |
const navigation = useNavigation(); | ||
const [isCameraActive, setIsCameraActive] = useState(navigation.isFocused()); | ||
|
||
const isInTabNavigator = cameraTabIndex > -1; | ||
// Get the animation value from the tab navigator. Its a value between 0 and the | ||
// number of pages we render in the tab navigator. When we even just slightly start to scroll to the camera page, | ||
// (value is e.g. 0.001 on animation start) we want to activate the camera, so its as fast as possible active. | ||
const tabPositionAnimation = useTabAnimation(); | ||
// We can use a condition to call a hook here because otherwise the hook throws | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
const tabPositionAnimation = isInTabNavigator ? useTabAnimation() : null; | ||
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. while this may be working, it is not the correct way of using hooks. An alternative would be to create two screens. One for direct access and one for in-tab selector. Define these in routes.
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 prefer the second option. Creating that much boilerplate just for not calling a hook conditionally isn't worth it IMO. 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. So, I checked and it seems 1-2 are already done in the code. We only need to do 3-4. 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. The main component used is 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 won't recommend the second option has it will lead to the same issue which we prevented by throwing the error. We want to prevent he user from using this hook in non tab screens. |
||
|
||
useEffect(() => { | ||
if (!isInTabNavigator) { | ||
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. We skipped this hook, did you check if the camera is opening? I think we will have to add a different hook to activate the camera when not inside Tab Selector. 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. |
||
return; | ||
} | ||
|
||
const listenerId = tabPositionAnimation.addListener(({value}) => { | ||
// Activate camera as soon the index is animating towards the `cameraTabIndex` | ||
setIsCameraActive(value > cameraTabIndex - 1 && value < cameraTabIndex + 1); | ||
|
@@ -33,7 +40,7 @@ function NavigationAwareCamera({cameraTabIndex, forwardedRef, ...props}) { | |
return () => { | ||
tabPositionAnimation.removeListener(listenerId); | ||
}; | ||
}, [cameraTabIndex, tabPositionAnimation]); | ||
}, [cameraTabIndex, isInTabNavigator, tabPositionAnimation]); | ||
|
||
// Note: The useEffect can be removed once VisionCamera V3 is used. | ||
// Its only needed for android, because there is a native cameraX android bug. With out this flow would break the camera: | ||
|
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 is a workaround to detect if we are in a tab.
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.
Agree, but I don't know I prefer
isTab
or something similar to proposal 2. What do you think?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.
Even if we use proposal 2 is comes down to conditional use of hook.