Loading...

How to get the current path in Next.js?

question nextjs react
Ram Patra Published on July 19, 2024
Next.js 13 onwards

To get the current path in a Next.js application, you can use the usePathname hook from next/navigation module. This hook allows you to access the current pathname within your components. Here’s how you can use it:

  1. Install Next.js: Make sure you have Next.js installed. If you don’t, you can set up a new Next.js project by running:

    npx create-next-app@latest
    
  2. Create or Update a Component: Use the usePathname hook in your component to get the current path. Below is an example of how to do this:

    // Import the necessary modules
    import { usePathname } from 'next/navigation';
    
    const CurrentPath = () => {
      // Use the usePathname hook to get the current path
      const pathname = usePathname();
    
      // Render the current path
      return (
        <div>
          <h1>Current Path</h1>
          <p>{pathname}</p>
        </div>
      );
    };
    
    export default CurrentPath;
    
  3. Use the Component: You can now use this component in any part of your Next.js application where you need to display or use the current path.

Here’s a more complete example to demonstrate how you might include this in a Next.js page:

// app/page.jsx
import CurrentPath from '../components/CurrentPath';

const HomePage = () => {
  return (
    <div>
      <h1>Home Page</h1>
      <CurrentPath />
    </div>
  );
};

export default HomePage;

In this example:

  • usePathname is imported from next/navigation.
  • usePathname hook is called within the CurrentPath component to get the current path.
  • The current path is displayed within a <p> tag.

This will dynamically show the current path whenever the component is rendered, which is useful for navigation highlights, breadcrumbs, or any feature dependent on the current route in your Next.js application.

One example can be to conditionally show a button like below:

{pathname === '/' && (
  <button onClick={handleClick}>About Button</button>
)}
{pathname === '/about' && (
  <button onClick={handleClick}>Home Button</button>
)}

Note: The component has to be a client component in order to use the usePathname hook.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

KeyScreen

Show keypresses on your screen.

ToDoBar

Your to-dos on your menu bar.

SimpleFill

Fill forms using your right-click menu.

IconSim

Preview your Mac app icons.

Ram Patra Published on July 19, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question nextjs react November 17, 2024 How to define metadata in a client component in Nextjs?

Nextjs does not allow defining the metadata object in client components. There are a couple of ways to resolve this problem. I will be talking about two of them here.

question typescript react September 28, 2024 How to add a custom element to a Next.js/Typescript project?

Let’s say I have a custom element setapp-badge that I want to use in my tsx files. If I go ahead and use it, the compiler will throw an error and Next.js will fail to build. It seems the problem might be a combination of how Next.js, TypeScript, and custom elements work together. Therefore, let’s try an approach that avoids the namespace/module issues while ensuring custom elements are recognized in a Next.js/TypeScript project.

question nextjs vercel March 1, 2025 Three ways to disable Image Optimization in Vercel

If you’re using the free tier of Vercel, like I do for some of my small side projects, you will get the 402 Payment Required error eventually when loading images. This is because you have hit the Image Optimization limit of the free plan. The solution to this is to disable Vercel’s Image Optimization, or to host the images elsewhere, or to upgrade to their Pro plan.

Like my work?

Please, feel free to reach out. I would be more than happy to chat.