1import { useRouter } from 'next/router'
2
3function ActiveLink({ children, href }) {
4 const router = useRouter()
5
6 const handleClick = (e) => {
7 e.preventDefault()
8 router.push(href)
9 }
10
11 return (
12 <a href={href} onClick={handleClick} style={style}>
13 {children}
14 </a>
15 )
16}
17
18export default ActiveLink
19
1function getFullUrl(req, fallback) {
2 //server side request object(req)
3 if(req) {
4 return req.protocol + '://' + req.get('host') + req.originalUrl
5
6 } //making sure we are on the client side
7 else if(!(typeof window === 'undefined')) {
8 return window.location.href
9
10 } else {
11 return fallback
12 }
13}
14
15//usage on nextjs
16static async getInitialProps({req}) {
17 let fullUrl = getFullUrl(req, "")//gets the full url or fallback to ""
18 return { fullUrl: fullUrl }
19}