Answers for "nextjs navigate to a page through a function"

3

router nextjs

import { useRouter } from 'next/router'

function ActiveLink({ children, href }) {
  const router = useRouter()

  const handleClick = (e) => {
    e.preventDefault()
    router.push(href)
  }

  return (
    <a href={href} onClick={handleClick} style={style}>
      {children}
    </a>
  )
}

export default ActiveLink
Posted by: Guest on November-16-2020
0

nextjs router get complete url

function getFullUrl(req, fallback) {
  //server side request object(req)
  if(req) {
    return req.protocol + '://' + req.get('host') + req.originalUrl
    
  } //making sure we are on the client side
  else if(!(typeof window === 'undefined')) {
    return window.location.href
    
  } else {
    return fallback
  }
}

//usage on nextjs
static async getInitialProps({req}) {
   let fullUrl = getFullUrl(req, "")//gets the full url or fallback to ""
    return { fullUrl: fullUrl }
}
Posted by: Guest on January-29-2021

Code answers related to "nextjs navigate to a page through a function"

Code answers related to "Javascript"

Browse Popular Code Answers by Language