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

chore: migrate withNavigationFallback.js class to function component #27388

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Changes from all 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
46 changes: 19 additions & 27 deletions src/components/withNavigationFallback.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
import React, {Component} from 'react';
import React, {forwardRef, useContext, useMemo} from 'react';
import {NavigationContext} from '@react-navigation/core';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import refPropTypes from './refPropTypes';

export default function (WrappedComponent) {
class WithNavigationFallback extends Component {
render() {
if (!this.context) {
return (
<NavigationContext.Provider
value={{
isFocused: () => true,
addListener: () => () => {},
removeListener: () => () => {},
}}
>
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
ref={this.props.forwardedRef}
/>
</NavigationContext.Provider>
);
}
function WithNavigationFallback(props) {
const context = useContext(NavigationContext);

return (
const navigationContextValue = useMemo(() => ({isFocused: () => true, addListener: () => () => {}, removeListener: () => () => {}}), []);

return context ? (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={props.forwardedRef}
/>
) : (
<NavigationContext.Provider value={navigationContextValue}>
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...this.props}
ref={this.props.forwardedRef}
{...props}
ref={props.forwardedRef}
/>
);
}
</NavigationContext.Provider>
);
}
WithNavigationFallback.contextType = NavigationContext;
WithNavigationFallback.displayName = `WithNavigationFocusWithFallback(${getComponentDisplayName(WrappedComponent)})`;
WithNavigationFallback.propTypes = {
forwardedRef: refPropTypes,
};
WithNavigationFallback.defaultProps = {
forwardedRef: undefined,
};
return React.forwardRef((props, ref) => (

return forwardRef((props, ref) => (
<WithNavigationFallback
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
Expand Down
Loading