Skip to content

Commit 7b25c8e

Browse files
authored
feat: add getCurrentRoute (facebook#8254)
1 parent 9304a8a commit 7b25c8e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

packages/core/src/BaseNavigationContainer.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,17 @@ const BaseNavigationContainer = React.forwardRef(
199199
return getStateForRoute('root');
200200
}, [getStateForRoute]);
201201

202+
const getCurrentRoute = React.useCallback(() => {
203+
let state = getRootState();
204+
if (state === undefined) {
205+
return undefined;
206+
}
207+
while (state.routes[state.index].state !== undefined) {
208+
state = state.routes[state.index].state as NavigationState;
209+
}
210+
return state.routes[state.index];
211+
}, [getRootState]);
212+
202213
const emitter = useEventEmitter();
203214

204215
React.useImperativeHandle(ref, () => ({
@@ -221,6 +232,7 @@ const BaseNavigationContainer = React.forwardRef(
221232
getRootState,
222233
dangerouslyGetState: () => state,
223234
dangerouslyGetParent: () => undefined,
235+
getCurrentRoute,
224236
}));
225237

226238
const builderContext = React.useMemo(

packages/core/src/__tests__/index.test.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,3 +1490,41 @@ it("doesn't throw if children is null", () => {
14901490

14911491
expect(() => render(element).update(element)).not.toThrowError();
14921492
});
1493+
1494+
it('returns currently focused route with getCurrentRoute', () => {
1495+
const TestNavigator = (props: any): any => {
1496+
const { state, descriptors } = useNavigationBuilder(MockRouter, props);
1497+
1498+
return descriptors[state.routes[state.index].key].render();
1499+
};
1500+
1501+
const TestScreen = () => null;
1502+
1503+
const navigation = React.createRef<NavigationContainerRef>();
1504+
1505+
const container = (
1506+
<BaseNavigationContainer ref={navigation}>
1507+
<TestNavigator>
1508+
<Screen name="bar" options={{ a: 'b' }}>
1509+
{() => (
1510+
<TestNavigator initialRouteName="bar-a">
1511+
<Screen
1512+
name="bar-a"
1513+
component={TestScreen}
1514+
options={{ sample: 'data' }}
1515+
/>
1516+
</TestNavigator>
1517+
)}
1518+
</Screen>
1519+
<Screen name="xux" component={TestScreen} />
1520+
</TestNavigator>
1521+
</BaseNavigationContainer>
1522+
);
1523+
1524+
render(container).update(container);
1525+
1526+
expect(navigation.current?.getCurrentRoute()).toEqual({
1527+
key: 'bar-a',
1528+
name: 'bar-a',
1529+
});
1530+
});

packages/core/src/types.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ export type NavigationContainerRef = NavigationHelpers<ParamListBase> &
422422
* Get the rehydrated navigation state of the navigation tree.
423423
*/
424424
getRootState(): NavigationState;
425+
/**
426+
* Get the currently focused navigation route.
427+
*/
428+
getCurrentRoute(): Route<string> | undefined;
425429
};
426430

427431
export type TypedNavigator<

0 commit comments

Comments
 (0)