🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 5 Min Lesezeit
0

Next.js Tutorial : Adding Navbar with active Styling in a Server Component Layout

↗ Quelle (dev.to)
🗣️ Stimme:

The recent and major update in Next.js is the App router.



App router provides and facilitate a way to structure your pages in a reusable manner , such example are like not-found.tsx , loading.tsx and layout.tsx.



Every web app have some common pieces that are need to available in all the page of the application or parts of it.



Such one example is Navigation bar.



With Nextjs , we can leverage the use of layout.tsx to add Navigation bar in all the pages.



In nextjs , all the pages are by default server-components



And if we are looking to use layout.tsx , we need to make sure that its a server component as well because layout.tsx will wrap all the other sub route pages ex: /about , /user , /settings.



The navbar that we want to include in a layout will be a client-component and the reason behind is that it will involve client interaction (clicking) and also we need to have active state.



So this is how the component hierarchy will be structured.








Let's breakdown the navbar.tsx and understand its bits and pieces.






1. Make this a client component using "use client";






2. Setting Up Navigation Items



Define your navigation items in an array. Each item includes an id, label, and href.




CODE
const navItems = [
{
id: "home",
label: "Home",
href: "/home",
},
{
id: "about",
label: "About",
href: "/about",
},
{
id: "settings",
label: "Settings",
href: "/settings",
},
];










3. Using usePathname to Get the Current Path



Next.js provides the usePathname hook from next/navigation to retrieve the current path. You can use this to determine if a link is active.




CODE

import { usePathname } from "next/navigation";
import { Url } from "next/dist/shared/lib/router/router";

const isActive = (path: Url) => pathname === path;










4. Iterate through all the navItems in the component






CODE

<ul className="flex justify-end items-center gap-4">
{navItems.map((eachItem) => (
<li key={eachItem.id}>
<Link
href={eachItem.href}
className={`${
isActive(eachItem.href) ? "text-spotify-green" : ""
}`}
>
{eachItem.label}
</Link>
</li>
))}
</ul>










5. Styling Active Links



In the code above, the isActive function checks if the current path matches the link’s href. When active, a specific class (text-spotify-green) is added to style the link.




CODE

className={`${
isActive(eachItem.href) ? "text-spotify-green" : ""
}`}










Lets add the navbar.tsx component to our layout.tsx






CODE
import type { Metadata } from "next";
import "./globals.css";
import Head from "next/head";
import { Poppins } from "next/font/google";
import Navbar from "@src/components/Navbar";

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

const poppins = Poppins({
subsets: ["latin"],
weight: ["100", "200", "300", "400", "500", "600", "700", "800", "900"],
display: "swap",
variable: "--font-poppins",
});

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en" className={`${poppins.variable}`}>
<Head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link
href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap"
rel="stylesheet"
/>
</Head>
<body className="font-poppins antialiased p-4 container md:mx-auto md:p-10 bg-spotify-black">
<Navbar/>
{children}
</body>
</html>
);
}







So this is how we can implement navbar in server layout component in nextjs .



Hope this was helpful , thanks!!

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Next.js Tutorial : Adding Navbar with active Styling in a Server Component Layout

Thematisch verwandte Begriffe: Nextjs, Tutorial, Adding, Navbar · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...