Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[router] fix relative navigation on hoisted routes #27778

Merged
merged 3 commits into from Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/expo-router/CHANGELOG.md
Expand Up @@ -29,6 +29,7 @@
- Export `toHaveRouterState` and other matcher types from `expo-router/testing-library` ([#27646](https://github.com/expo/expo/pull/27646) by [@marklawlor](https://github.com/marklawlor))
- Fix missing types from typed routes ([#27412](https://github.com/expo/expo/pull/27412) by [@marklawlor](https://github.com/marklawlor))
- Fork NavigationContainer on web to use custom linking context ([#27712](https://github.com/expo/expo/pull/27712) by [@marklawlor](https://github.com/marklawlor))
- Fix relative navigation on hoisted routes ([#27778](https://github.com/expo/expo/pull/27778) by [@marklawlor](https://github.com/marklawlor))

### 💡 Others

Expand Down
2 changes: 1 addition & 1 deletion packages/expo-router/build/LocationProvider.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion packages/expo-router/build/LocationProvider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/expo-router/build/LocationProvider.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion packages/expo-router/src/LocationProvider.tsx
Expand Up @@ -33,10 +33,21 @@ function isIndexPath(state: State) {
if (route.state) {
return isIndexPath(route.state);
}
// router.params is typed as 'object', so this usual syntax is to please TypeScript

// Index routes on the same level as a layout do not have `index` in their name
if (route.params && 'screen' in route.params) {
return route.params.screen === 'index';
}

// The `params` key will not exist if there are no params
// So we need to do a positive lookahead to check if the route ends with /index
// Nested routes that are hoisted will have a name ending with /index
// e.g name could be /user/[id]/index
if (route.name.match(/.+\/index$/)) return true;

// The state will either have params (because there are multiple _layout) or it will be hoisted with a name
// If we don't match the above cases, then it's not an index route

return false;
}

Expand Down
20 changes: 19 additions & 1 deletion packages/expo-router/src/__tests__/navigation.test.ios.tsx
@@ -1,5 +1,5 @@
/* eslint-disable react-hooks/rules-of-hooks */
import React, { Text } from 'react-native';
import React, { Text, View } from 'react-native';

import {
useRouter,
Expand All @@ -9,6 +9,7 @@ import {
Redirect,
Slot,
usePathname,
Link,
} from '../exports';
import { Stack } from '../layouts/Stack';
import { Tabs } from '../layouts/Tabs';
Expand Down Expand Up @@ -821,6 +822,23 @@ it('can push relative links from index routes', async () => {
expect(screen).toHavePathname('/test/bar');
});

it('can push relative links from hoisted routes', () => {
renderRouter(
{
_layout: () => <Stack />,
'parent/index': () => <Link testID="link" href="./child" />,
'parent/child': () => <View testID="child" />,
},
{
initialUrl: '/parent',
}
);

expect(screen.getByTestId('link')).toBeOnTheScreen();
fireEvent(screen.getByTestId('link'), 'press');
expect(screen.getByTestId('child')).toBeOnTheScreen();
});

it('can navigation to a relative route without losing path params', async () => {
renderRouter(
{
Expand Down