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

[No QA] [TS Migration] migrate withNavigation.js to TypeScript #29531

Merged
merged 6 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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'],
},
],
'@typescript-eslint/ban-types': [
Expand Down
40 changes: 0 additions & 40 deletions src/components/withNavigation.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/components/withNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React, {ComponentType, ForwardedRef, RefAttributes} from 'react';
import {NavigationProp, useNavigation} from '@react-navigation/native';
import getComponentDisplayName from '../libs/getComponentDisplayName';

type WithNavigationProps = {
navigation: NavigationProp<ReactNavigation.RootParamList>;
};

export default function withNavigation<TProps extends WithNavigationProps, TRef>(WrappedComponent: ComponentType<TProps & RefAttributes<TRef>>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Add return type for this function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

function WithNavigation(props: Omit<TProps, keyof WithNavigationProps>, ref: ForwardedRef<TRef>) {
const navigation = useNavigation();
return (
<WrappedComponent
{...(props as TProps)}
ref={ref}
navigation={navigation}
/>
);
}

WithNavigation.displayName = `withNavigation(${getComponentDisplayName(WrappedComponent as ComponentType)})`;
Copy link
Contributor

Choose a reason for hiding this comment

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

@JKobrynski Maybe you can add same update to getComponentDisplayName to get rid of the type assertion 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.

Done!

return React.forwardRef(WithNavigation);
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import {View} from 'react-native';
import {createStackNavigator} from '@react-navigation/stack';
import PropTypes from 'prop-types';

import * as ModalStackNavigators from '../ModalStackNavigators';
import RHPScreenOptions from '../RHPScreenOptions';
import useWindowDimensions from '../../../../hooks/useWindowDimensions';
import {withNavigationPropTypes} from '../../../../components/withNavigation';
import styles from '../../../../styles/styles';
import Overlay from './Overlay';
import NoDropZone from '../../../../components/DragAndDrop/NoDropZone';

const Stack = createStackNavigator();

const propTypes = {
...withNavigationPropTypes,
navigation: PropTypes.object.isRequired,
};

function RightModalNavigator(props) {
Expand Down
3 changes: 1 addition & 2 deletions src/libs/Navigation/AppNavigator/ReportScreenIDSetter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {withOnyx} from 'react-native-onyx';
import ONYXKEYS from '../../../ONYXKEYS';
import * as ReportUtils from '../../ReportUtils';
import reportPropTypes from '../../../pages/reportPropTypes';
import {withNavigationPropTypes} from '../../../components/withNavigation';
import * as App from '../../actions/App';
import usePermissions from '../../../hooks/usePermissions';
import CONST from '../../../CONST';
Expand Down Expand Up @@ -40,7 +39,7 @@ const propTypes = {
}),
}).isRequired,

...withNavigationPropTypes,
navigation: PropTypes.object.isRequired,
};

const defaultProps = {
Expand Down
3 changes: 1 addition & 2 deletions src/libs/Navigation/AppNavigator/ReportScreenWrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import PropTypes from 'prop-types';
import React from 'react';
import {withNavigationPropTypes} from '../../../components/withNavigation';
import ReportScreen from '../../../pages/home/ReportScreen';
import ReportScreenIDSetter from './ReportScreenIDSetter';

Expand All @@ -17,7 +16,7 @@ const propTypes = {
}),
}).isRequired,

...withNavigationPropTypes,
navigation: PropTypes.object.isRequired,
};

const defaultProps = {};
Expand Down
Loading