Active Links & Conditional Rendering
Active Links & Conditional Rendering
1. Implement Active Link Styling
a. Import the usePathname Hook
usePathname Hookimport { usePathname } from 'next/navigation';
b. Retrieve the Current Pathname
const pathname = usePathname();
c. Apply Conditional Styling to Each Link
<Link href="/" className={`${pathname === '/' ? 'bg-black' : ''} px-3 py-2 text-white hover:bg-gray-900 rounded-md`} > Home </Link><Link href="/properties"> className={`${pathname === '/properties' ? 'bg-black' : ''} px-3 py-2 text-white hover:bg-gray-900 rounded-md`} > Properties </Link><Link href="/properties/add"> className={`${pathname === '/properties/add' ? 'bg-black' : ''} px-3 py-2 text-white hover:bg-gray-900 rounded-md`} > Add Properties </Link>
2. Implement Conditional Rendering Based on Authentication State
a. Set Up Authentication State
b. Conditionally Render Navbar Items
3. Testing and Final Adjustments
a. Run Your Development Server


b. Toggle the isLoggedIn State
isLoggedIn Statec. Debug and Refine CSS Classes
Recap
Last updated