Skip to content

Commit

Permalink
useMemo for navigation context value
Browse files Browse the repository at this point in the history
  • Loading branch information
teneeto committed Sep 14, 2023
1 parent 48ff412 commit 6f197b9
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/components/withNavigationFallback.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {forwardRef, useContext} from 'react';
import React, {forwardRef, useContext, useMemo} from 'react';
import {NavigationContext} from '@react-navigation/core';
import getComponentDisplayName from '../libs/getComponentDisplayName';
import refPropTypes from './refPropTypes';
Expand All @@ -7,26 +7,22 @@ export default function (WrappedComponent) {
function WithNavigationFallback(props) {
const context = useContext(NavigationContext);

return !context ? (
<NavigationContext.Provider
value={{
isFocused: () => true,
addListener: () => () => {},
removeListener: () => () => {},
}}
>
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
{...props}
ref={props.forwardedRef}
/>
</NavigationContext.Provider>
) : (
<WrappedComponent
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
ref={props.forwardedRef}
/>
);
}
WithNavigationFallback.displayName = `WithNavigationFocusWithFallback(${getComponentDisplayName(WrappedComponent)})`;
Expand Down

0 comments on commit 6f197b9

Please sign in to comment.