-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[No QA] [TS migration] Migrate 'withNavigationFallback.js' HOC to TypeScript #30072
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
Changes from all commits
7af7d79
9f79ac3
e06dde2
c0166dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import {NavigationContext} from '@react-navigation/core'; | ||
| import {NavigationProp} from '@react-navigation/native'; | ||
| import {ParamListBase} from '@react-navigation/routers'; | ||
| import React, {ComponentType, ForwardedRef, forwardRef, ReactElement, RefAttributes, useContext, useMemo} from 'react'; | ||
|
|
||
| type AddListenerCallback = () => void; | ||
|
|
||
| type RemoveListenerCallback = () => void; | ||
|
|
||
| type NavigationContextValue = { | ||
| isFocused: () => boolean; | ||
| addListener: () => AddListenerCallback; | ||
| removeListener: () => RemoveListenerCallback; | ||
| }; | ||
|
|
||
| export default function <TProps, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>): (props: TProps & RefAttributes<TRef>) => ReactElement | null { | ||
| function WithNavigationFallback(props: TProps, ref: ForwardedRef<TRef>) { | ||
| const context = useContext(NavigationContext); | ||
|
|
||
| const navigationContextValue: NavigationContextValue = useMemo( | ||
| () => ({ | ||
| isFocused: () => true, | ||
| addListener: () => () => {}, | ||
| removeListener: () => () => {}, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| return context ? ( | ||
| <WrappedComponent | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| ref={ref} | ||
| /> | ||
| ) : ( | ||
| <NavigationContext.Provider value={navigationContextValue as unknown as NavigationProp<ParamListBase>}> | ||
|
Contributor
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. Why do we need this assertion?
Contributor
Author
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. @fabioh8010 Because
Contributor
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'm trying to understand why do we pass only this values in the first place 🤔. It seems that only
Contributor
Author
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. @fabioh8010 Let me provide you with a little bit more context. For more details, see
Contributor
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. Ok I see, I guess we have no other option then |
||
| <WrappedComponent | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| {...props} | ||
| ref={ref} | ||
| /> | ||
| </NavigationContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| WithNavigationFallback.displayName = 'WithNavigationFocusWithFallback'; | ||
|
|
||
| return forwardRef(WithNavigationFallback); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.