diff --git a/packages/core/src/BaseNavigationContainer.tsx b/packages/core/src/BaseNavigationContainer.tsx index 9fff3274..913bf935 100644 --- a/packages/core/src/BaseNavigationContainer.tsx +++ b/packages/core/src/BaseNavigationContainer.tsx @@ -8,9 +8,11 @@ import { NavigationAction, } from '@react-navigation/routers'; import EnsureSingleNavigator from './EnsureSingleNavigator'; +import UnhandledActionContext from './UnhandledActionContext'; import NavigationBuilderContext from './NavigationBuilderContext'; import NavigationStateContext from './NavigationStateContext'; -import UnhandledActionContext from './UnhandledActionContext'; +import NavigationRouteContext from './NavigationRouteContext'; +import NavigationContext from './NavigationContext'; import { ScheduleUpdateContext } from './useScheduleUpdate'; import useChildListeners from './useChildListeners'; import useKeyedChildListeners from './useKeyedChildListeners'; @@ -397,7 +399,7 @@ const BaseNavigationContainer = React.forwardRef( [] ); - return ( + let element = ( @@ -410,6 +412,19 @@ const BaseNavigationContainer = React.forwardRef( ); + + if (independent) { + // We need to clear any existing contexts for nested independent container to work correctly + element = ( + + + {element} + + + ); + } + + return element; } ); diff --git a/packages/core/src/__tests__/BaseNavigationContainer.test.tsx b/packages/core/src/__tests__/BaseNavigationContainer.test.tsx index 62df51f8..15757cf6 100644 --- a/packages/core/src/__tests__/BaseNavigationContainer.test.tsx +++ b/packages/core/src/__tests__/BaseNavigationContainer.test.tsx @@ -757,3 +757,67 @@ it('invokes the unhandled action listener with the unhandled action', () => { type: 'NAVIGATE', }); }); + +it('works with state change events in independent nested container', () => { + const TestNavigator = (props: any) => { + const { state, descriptors } = useNavigationBuilder(MockRouter, props); + + return ( + + {state.routes.map((route) => descriptors[route.key].render())} + + ); + }; + + const ref = React.createRef(); + + const onStateChange = jest.fn(); + + render( + + + + {() => ( + + + {() => null} + {() => null} + + + )} + + {() => null} + + + ); + + act(() => ref.current?.navigate('lex')); + + expect(onStateChange).toBeCalledWith({ + index: 1, + key: '15', + routeNames: ['qux', 'lex'], + routes: [ + { key: 'qux', name: 'qux' }, + { key: 'lex', name: 'lex' }, + ], + stale: false, + type: 'test', + }); + + expect(ref.current?.getRootState()).toEqual({ + index: 1, + key: '15', + routeNames: ['qux', 'lex'], + routes: [ + { key: 'qux', name: 'qux' }, + { key: 'lex', name: 'lex' }, + ], + stale: false, + type: 'test', + }); +});