Skip to content

Commit

Permalink
Merge pull request #27388 from teneeto/chore/16223-migrate-with-navig…
Browse files Browse the repository at this point in the history
…ation-fallback-to-function-component

chore: migrate withNavigationFallback.js class to function component
  • Loading branch information
Li357 authored Sep 18, 2023
2 parents a37edd5 + 6f197b9 commit 29056dc
Showing 1 changed file with 19 additions and 27 deletions.
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

0 comments on commit 29056dc

Please sign in to comment.