question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: No selected layout segment below the current level

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
alex-marinovcommented, Oct 27, 2022

I’ve used useSelectedLayoutSegments() as a workaround;

let layoutSegments = useSelectedLayoutSegments();
let segment = layoutSegments[0];
if (segment === undefined) segment = "";
let active = href === `/${segment}`;
0reactions
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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blog - Layouts RFC | Next.js
A layout is UI that is shared between route segments in a subtree. Layouts do not affect URL paths and do not re-render...
Read more >
Discussion #37136 · vercel/next.js - RFC: Layouts - GitHub
If we call server code in the component level, can we still share it with parent/children segment layouts? Will there be a route...
Read more >
Error: The segments listed below contain the same selections ...
This message is caused by using the same segment code as another segment and/or selection(s). Currently the application does not allow for us...
Read more >
A Full Segment Implementation
Before you start implementing from your tracking plan, let's review the Segment methods, what they do, and when you should use each.
Read more >
SegmentedUI Widget
Segment widget is a container that provides the capability to define a row template that repeats for every row or page of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found