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

Error: No selected layout segment below the current level #41878

Closed
1 task done
TurniXXD opened this issue Oct 26, 2022 · 6 comments · Fixed by #42299
Closed
1 task done

Error: No selected layout segment below the current level #41878

TurniXXD opened this issue Oct 26, 2022 · 6 comments · Fixed by #42299
Assignees
Labels
bug Issue was opened via the bug report template.

Comments

@TurniXXD
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Wed, 05 Oct 2022 12:15:15 +0000
Binaries:
  Node: 16.16.0
  npm: 8.11.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 12.3.2-canary.43
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

Brave

How are you deploying your application? (if relevant)

No response

Describe the Bug

I was following guide from Next.js Conf (Stage J) for Next.js 13, and I keep getting this error Error: No selected layout segment below the current level when trying to use useSelectedLayoutSegment hook in client component

"use client"

import Link from 'next/link'
import { useSelectedLayoutSegment } from 'next/navigation'

export default function NavLink({ href, children }: React.PropsWithChildren<{ href: string }>) {
  let segment = useSelectedLayoutSegment();
  let active = href === `/${segment}`
  return <Link className={active && 'text-blue-700' || ''} href={href}>{children}</Link>
}

Expected Behavior

I can use useSelectedLayoutSegment hook in a way i believe it works in client component

Link to reproduction

https://github.com/TurniXXD/next-13

To Reproduce

  • Create new Next.js 13 app with typescript
  • I have enabled the experimental: { appDir: true } config
  • Add tailwindcss
  • Add new components and pages
  • Try to use hook in client component
@TurniXXD TurniXXD added the bug Issue was opened via the bug report template. label Oct 26, 2022
@stahlmanDesign
Copy link
Contributor

stahlmanDesign commented Oct 27, 2022

related to #41879

@alex-marinov
Copy link

having the same error - here is the repo that reproduces the issue: https://github.com/alex-marinov/nextjs13-testapp

@alex-marinov
Copy link

I've used useSelectedLayoutSegments() as a workaround;

let layoutSegments = useSelectedLayoutSegments();
let segment = layoutSegments[0];
if (segment === undefined) segment = "";
let active = href === `/${segment}`;

@chukwumaokere
Copy link

Above suggestion works for me as well. Wonder why this bug exists... but Sam Selikoff's video didnt encounter this

@alex-marinov
Copy link

Above suggestion works for me as well. Wonder why this bug exists... but Sam Selikoff's video didn't encounter this

I had a quick look at his repo, and looks like nextjs version he is using doesn't have the update that throws error.

I've managed to find the line that throws this error;

throw new Error('No selected layout segment below the current level')
< perhaps should need to return null, instead of throwing error?

ijjk pushed a commit that referenced this issue Nov 2, 2022
When `useSelectedLayoutSegment` is used on the current level or in a
page it should return null.

fixes #41879
fixes #41878

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
@github-actions
Copy link
Contributor

github-actions bot commented Dec 3, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants