next/router | Next.js

next/router

useRouter

import { useRouter } from 'next/router'

function ActiveLink({ children, href }) {
  const router = useRouter()
  const style = {
    marginRight: 10,
    color: router.asPath === href ? 'red' : 'black',
  }

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

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

export default ActiveLink

router object에 들어있는 것들

const router = useRouter();

useEffect(())=>{
	if(router.isReady){ // router가 준비 되었을때
	console.log(router.query)
}

}, [router.query])

<aside> 💡 router 객체에는 여러가지 있다.

</aside>

router.push() : history가 쌓이는 이동

router.push(url, as, { scroll : true})