diff --git a/packages/next/client/router.ts b/packages/next/client/router.ts index 7f4acbd51a60c..930535fb1de3b 100644 --- a/packages/next/client/router.ts +++ b/packages/next/client/router.ts @@ -133,7 +133,7 @@ export function useRouter(): NextRouter { const router = React.useContext(RouterContext) if (!router) { throw new Error( - 'Error: NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted' + 'NextRouter was not mounted. https://nextjs.org/docs/messages/next-router-not-mounted' ) } diff --git a/test/e2e/app-dir/app/app/compat-hooks/app/page.js b/test/e2e/app-dir/app/app/compat-hooks/app/page.js new file mode 100644 index 0000000000000..a72c90c528f94 --- /dev/null +++ b/test/e2e/app-dir/app/app/compat-hooks/app/page.js @@ -0,0 +1,6 @@ +'use client' +import { RouterHooksFixtures } from '../../../components/router-hooks-fixtures' + +export default function Page() { + return +} diff --git a/test/e2e/app-dir/app/app/router/page.js b/test/e2e/app-dir/app/app/router/page.js new file mode 100644 index 0000000000000..e8a3dbcd7ecc9 --- /dev/null +++ b/test/e2e/app-dir/app/app/router/page.js @@ -0,0 +1,10 @@ +'use client' + +import { useRouter } from 'next/router' + +export default function RouterPage() { + // eslint-disable-next-line no-unused-vars + const router = useRouter() + + return
You shouldn't see this
+} diff --git a/test/e2e/app-dir/app/components/router-hooks-fixtures.js b/test/e2e/app-dir/app/components/router-hooks-fixtures.js index 6c5772188b663..90c224f5d11a4 100644 --- a/test/e2e/app-dir/app/components/router-hooks-fixtures.js +++ b/test/e2e/app-dir/app/components/router-hooks-fixtures.js @@ -1,10 +1,10 @@ -import { useRouter as usePagesRouter } from 'next/router' +import { useRouter as usePagesRouter } from 'next/compat/router' import { usePathname, useRouter as useAppRouter, useSearchParams, } from 'next/compat/navigation' -import { useState, useEffect } from 'react' +import { useState, useEffect, useMemo } from 'react' export const RouterHooksFixtures = () => { const pagesRouter = usePagesRouter() @@ -12,21 +12,26 @@ export const RouterHooksFixtures = () => { const searchParams = useSearchParams() const pathname = usePathname() + const isReady = useMemo( + () => !pagesRouter || pagesRouter.isReady, + [pagesRouter] + ) + const [value, setValue] = useState(null) useEffect(() => { - if (!pagesRouter.isReady) { + if (!isReady) { return } - setValue(searchParams.get('key')) - }, [pagesRouter.isReady, searchParams]) + setValue(searchParams?.get('key') ?? null) + }, [isReady, searchParams]) const onClick = () => { - appRouter.push('/adapter-hooks/pushed') + appRouter.push('/compat-hooks/pushed') } return ( -
+
{value}
{pathname}