-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(navigation): allow
useSelectedLayoutSegment(s)
in Pages Router (#…
…62584) ### What? Do not fail when `useSelectedLayoutSegment` or `useSelectedLayoutSegments` APIs are called in the Pages Router. ### Why? This makes migration easier and creates consistency with our other App Router-specific APIs that inherit the same behavior. ### How? Similar to #47490, we return `null` if there is no Layout context (indicating being in Pages Router) Types are also overridden in the navigation compact module declaration which kicks in during start to correct the types if we detect a `pages/` directory. Note to reviewer: #47490 didn't add a test, so I added one top-level, let me know if you have a better suggestion for placing. Closes NEXT-2506 Fixes #61464
- Loading branch information
1 parent
c353b5f
commit 9ae437f
Showing
5 changed files
with
49 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
test/e2e/useselectedlayoutsegment-s-in-pages-router/pages/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { | ||
useSelectedLayoutSegment, | ||
useSelectedLayoutSegments, | ||
} from 'next/navigation' | ||
|
||
export default function Page() { | ||
useSelectedLayoutSegment() | ||
useSelectedLayoutSegments() | ||
return <p id="hello-world">Hello World</p> | ||
} |
14 changes: 14 additions & 0 deletions
14
...electedlayoutsegment-s-in-pages-router/useselectedlayoutsegment-s-in-pages-router.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
|
||
describe('useSelectedLayoutSegment(s) in Pages Router', () => { | ||
const { next } = nextTestSetup({ files: __dirname }) | ||
|
||
it('Should render with `useSelectedLayoutSegment(s) hooks', async () => { | ||
const browser = await next.browser('/') | ||
|
||
await browser.waitForElementByCss('#hello-world') | ||
expect(await browser.elementByCss('#hello-world').text()).toBe( | ||
'Hello World' | ||
) | ||
}) | ||
}) |