Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

583
Views
How to get the current full URL in Next.js/Typescript component?

I have a component that I need to get the current full URL in, here's the simplied version:

/**
 * Share dropdown component
 */
export const ShareDropdown: React.FC<{ className: string }> = ({
  className,
}) => {
  return (
    <div className={className}>{currentURL}</div>
  )
}

Did some Googling and saw some say use Next's router (but didn't see that return the domain), other using getInitialProps but didn't see how to incorporate into a functional component.

New to all of this, so just trying to find the best way, seems like it should be simple.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use simply useRouter from nextjs just like this

import { useRouter } from 'next/router';
...
const { asPath } = useRouter();
...
return (
   <div className={className}>http://example.com{asPath}</div>
)

Additionally you can save http://example.com in your environment variables

If you have configured a base path you need to get it

 import { useRouter, basePath } from 'next/router';
 

More info about useRouter here

about 4 years ago · Juan Pablo Isaza Report

0

Store window.location.href as a variable. so, like: const URL = window.location.href and then simply put {URL} here:

/**
 * Share dropdown component
 */
export const ShareDropdown: React.FC<{ className: string }> = ({
  className,
}) => {
const URL = window.location.href
  return (
    <div className={className}>{URL}</div>
  )
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!