#Explanations next doc example

1 messages · Page 1 of 1 (latest)

vague hatchBOT
#

Hi @cloud glade. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Hello! Your understanding of parallel routes is correct. Parallel routes in Next.js allow you to simultaneously or conditionally render one or more pages within the same layout. They are particularly useful for highly dynamic sections of an app, such as dashboards and feeds on social sites source (https://nextjs.org/docs/app/building-your-application/routing/parallel-routes).

In the example you mentioned, the login modal can indeed be opened from both the /dashboard page and the /dashboard/login page. This is achieved by using the @auth slot in the shared layout. The @auth slot is not a route segment and does not affect the URL structure. It's used to conditionally render the login modal based on whether the /login route is active source (https://nextjs.org/docs/app/building-your-application/routing/parallel-routes#modals).

The advantage of using parallel routes over traditional components is that they provide a more flexible and dynamic way to manage your application's UI. With parallel routes, you can:

#

However, it's important to note that parallel routes are meant to be used within a shared layout. Currently, Next.js does not support creating a separate layout.tsx file per slot source (https://github.com/vercel/next.js/issues/58506).

I hope this helps! If you have any more questions, feel free to ask.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@cloud glade kapa.ai is still learning and improving, please let us know how it did by reacting below

cloud glade
#

@vague hatch okay thank you, but I mean, what's the point of opening the logging modal when you're already in the login page ? Is there a way to have the same overall behavior but without the link when /login page is rendered ?
Maybe https://nextjs.org/docs/app/building-your-application/routing/parallel-routes#useselectedlayoutsegments ? But this is a client hook

Simultaneously render one or more pages in the same view that can be navigated independently. A pattern for highly dynamic applications.

vague hatchBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you're on the right track! You can indeed use the useSelectedLayoutSegment hook to determine the current active route segment and conditionally render the login link based on that. This hook can be used in a client component to read the active route segment within a slot source (https://nextjs.org/docs/app/building-your-application/routing/parallel-routes#useselectedlayoutsegments).

Here's an example of how you might use it:

import Link from 'next/link'
import { useSelectedLayoutSegment } from 'next/navigation'
 
export default function Layout({
 auth,
 children,
}: {
 auth: React.ReactNode
 children: React.ReactNode
}) {
 const loginSegment = useSelectedLayoutSegment('auth')
 
 return (
 <>
 <nav>
 {loginSegment !== 'login' && <Link href="/login">Open modal</Link>}
 </nav>
 <div>{auth}</div>
 <div>{children}</div>
 </>
 )
}

In this example, the Open modal link will only be rendered if the current active route segment is not login. This means that the link will not be displayed on the /login page, but it will be displayed on all other pages.

Remember, useSelectedLayoutSegment is a client-side hook, so it should be used in a client component. This means that the component using this hook will be server-side rendered by Next.js, but the JavaScript for the component will also be bundled and sent to the client, allowing for dynamic changes based on client-side transitions source (https://github.com/vercel/next.js/issues/43704).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: