Skip to content

Commit

Permalink
Early return
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Feb 9, 2023
1 parent b8e8186 commit 20027c1
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions packages/components/src/navigator/navigator-provider/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ function UnconnectedNavigatorProvider(
const [ screens, dispatch ] = useReducer( screensReducer, [] );
const currentMatch = useRef< MatchedPath >();
const matchedPath = useMemo( () => {
let currentPath: string | undefined;
if (
locationHistory.length === 0 ||
( currentPath =
locationHistory[ locationHistory.length - 1 ].path ) ===
undefined
) {
currentMatch.current = undefined;
return undefined;
}

const resolvePath = ( path: string ) => {
const newMatch = patternMatch( path, screens );

Expand All @@ -89,17 +100,9 @@ function UnconnectedNavigatorProvider(
return newMatch;
};

if ( locationHistory.length > 0 ) {
const path = locationHistory[ locationHistory.length - 1 ].path;
if ( path !== undefined ) {
const newMatch = resolvePath( path );
currentMatch.current = newMatch;
return newMatch;
}
}

currentMatch.current = undefined;
return undefined;
const newMatch = resolvePath( currentPath );
currentMatch.current = newMatch;
return newMatch;
}, [ screens, locationHistory ] );

const addScreen = useCallback(
Expand Down

0 comments on commit 20027c1

Please sign in to comment.