Skip to content

Commit

Permalink
Update react-navigation StrictMode patch to work also for native apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicu committed Jul 2, 2024
1 parent 551c6cb commit fee8ca3
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,48 @@ index 051520b..6fb49e0 100644
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
diff --git a/node_modules/@react-navigation/core/src/useNavigationBuilder.tsx b/node_modules/@react-navigation/core/src/useNavigationBuilder.tsx
index b1971ba..7d550e0 100644
--- a/node_modules/@react-navigation/core/src/useNavigationBuilder.tsx
+++ b/node_modules/@react-navigation/core/src/useNavigationBuilder.tsx
@@ -362,11 +362,6 @@ export default function useNavigationBuilder<

const stateCleanedUp = React.useRef(false);

- const cleanUpState = React.useCallback(() => {
- setCurrentState(undefined);
- stateCleanedUp.current = true;
- }, [setCurrentState]);
-
const setState = React.useCallback(
(state: NavigationState | PartialState<NavigationState> | undefined) => {
if (stateCleanedUp.current) {
@@ -540,6 +535,9 @@ export default function useNavigationBuilder<
state = nextState;

React.useEffect(() => {
+ // In strict mode, React will double-invoke effects.
+ // So we need to reset the flag if component was not unmounted
+ stateCleanedUp.current = false;
setKey(navigatorKey);

if (!getIsInitial()) {
@@ -551,14 +549,10 @@ export default function useNavigationBuilder<

return () => {
// We need to clean up state for this navigator on unmount
- // We do it in a timeout because we need to detect if another navigator mounted in the meantime
- // For example, if another navigator has started rendering, we should skip cleanup
- // Otherwise, our cleanup step will cleanup state for the other navigator and re-initialize it
- setTimeout(() => {
- if (getCurrentState() !== undefined && getKey() === navigatorKey) {
- cleanUpState();
- }
- }, 0);
+ if (getCurrentState() !== undefined && getKey() === navigatorKey) {
+ setCurrentState(undefined);
+ stateCleanedUp.current = true;
+ }
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

0 comments on commit fee8ca3

Please sign in to comment.