diff --git a/packages/core/src/useNavigationBuilder.tsx b/packages/core/src/useNavigationBuilder.tsx index ac85126c..e45783aa 100644 --- a/packages/core/src/useNavigationBuilder.tsx +++ b/packages/core/src/useNavigationBuilder.tsx @@ -299,12 +299,32 @@ export default function useNavigationBuilder< const { state: currentState, getState: getCurrentState, - setState, + setState: setCurrentState, setKey, getKey, getIsInitial, } = React.useContext(NavigationStateContext); + const stateCleanedUp = React.useRef(false); + + const cleanUpState = React.useCallback(() => { + setCurrentState(undefined); + stateCleanedUp.current = true; + }, [setCurrentState]); + + const setState = React.useCallback( + (state: NavigationState | PartialState | undefined) => { + if (stateCleanedUp.current) { + // State might have been already cleaned up due to unmount + // We do not want to expose API allowing to override this + // This would lead to old data preservation on main navigator unmount + return; + } + setCurrentState(state); + }, + [setCurrentState] + ); + const [initializedState, isFirstStateInitialization] = React.useMemo(() => { // If the current state isn't initialized on first render, we initialize it // We also need to re-initialize it if the state passed from parent was changed (maybe due to reset) @@ -444,7 +464,7 @@ export default function useNavigationBuilder< // Otherwise, our cleanup step will cleanup state for the other navigator and re-initialize it setTimeout(() => { if (getCurrentState() !== undefined && getKey() === navigatorKey) { - setState(undefined); + cleanUpState(); } }, 0); };