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

Remove 'withNavigationFallback.js' HOC #29449

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = {
},
{
selector: ['parameter', 'method'],
format: ['camelCase'],
format: ['camelCase', 'PascalCase'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we using pascal case for method parameters here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@akinwale It's left from TS migration. It shouldn't hurt, though, cause it's necessary for other HOCs anyway where we have WrappedComponent as a param (and it's in CamelCase). But I can remove it from this PR if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, makes sense to leave it in if it's going to be used it in the future. Just wanted to make sure. Thanks.

},
],
'@typescript-eslint/ban-types': [
Expand Down
43 changes: 0 additions & 43 deletions src/components/withNavigationFallback.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/components/withNavigationFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, {ComponentType, RefAttributes, ForwardedRef, forwardRef, useContext, useMemo} from 'react';
import {NavigationContext} from '@react-navigation/core';
import {NavigationProp} from '@react-navigation/native';
import {ParamListBase} from '@react-navigation/routers';
import getComponentDisplayName from '../libs/getComponentDisplayName';

type AddListenerCallback = () => void;

type RemoveListenerCallback = () => void;

type NavigationContextValue = {
isFocused: () => boolean;
addListener: () => AddListenerCallback;
removeListener: () => RemoveListenerCallback;
};

export default function <TProps, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>) {
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>}>
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={ref}
/>
</NavigationContext.Provider>
);
}

WithNavigationFallback.displayName = `WithNavigationFocusWithFallback(${getComponentDisplayName(WrappedComponent)})`;

return forwardRef(WithNavigationFallback);
}
2 changes: 1 addition & 1 deletion src/libs/getComponentDisplayName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentType} from 'react';

/** Returns the display name of a component */
export default function getComponentDisplayName(component: ComponentType): string {
export default function getComponentDisplayName<TProps>(component: ComponentType<TProps>): string {
return component.displayName ?? component.name ?? 'Component';
}
Loading