DEV Community

Cover image for Parallel Routes: Starter Guide in Nextjs
Patrick Dev
Patrick Dev

Posted on • Edited on

Parallel Routes: Starter Guide in Nextjs

◼️ Next.js 13.3 add a lot of features

And one of which completely extends the basic optics of React root file with the classic and single children, as below

// app/layout.tsx

import type { PropsWithChildren } from 'react';

export default ({ children }: PropsWithChildren) => {
    return (
        <html lang="en" className="dark">
            <body>
                <div id="root">{children}</div>
            </body>
        </html>
    );
};
Enter fullscreen mode Exit fullscreen mode

was the parallel routes, with the idea of having multiple ReactNode children and especially independent pages, in terms of navigation and rendering, in one and the same view ( or better with same layout ) and in rare cases used for conditional rendering of pages, to overcome and expand this barrier.

◼️ Well, that's exactly the purpose parallel routes

// app/layout.tsx

import type { PropsWithChildren } from 'react';

export default ({ outlines, children: content, ads }: Props) => {
    return (
        <html lang="en" className="dark">
            <body>
                <div id="root">
                    {outlines}
                    {content}
                    {ads}
                </div>
            </body>
        </html>
    );
};

interface Props extends PropsWithChildren {
    ads: React.ReactNode;
    outlines: React.ReactNode;
}
Enter fullscreen mode Exit fullscreen mode

OK, sounds good right?
But how to define these damn parallel routes?

◼️ Syntax of parallel routes

Each of the new “children” we can define it as a “slot” in technical terms, and to define them is simply by creating a new folder, in the app router directory of course, with the following syntax @youtslotname,

To conclude, I leave you with the structure for defining slots that I showed you in the code example mentioned earlier:

app
├── @ads
   └── page.tsx
├── @outlines
   └── page.tsx
├── layout.tsx
└── page.tsx
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay