Next.js Discord

Discord Forum

Using the same function of <Link> but from a function

Answered
larkx posted this in #help-forum
Open in Discord
Avatar
larkxOP
Hi, due to me wanting to link a table row, I can't use the traditional <Link> component. However, I can use an onClick event to achieve the same function. Is there a function in Nextjs that can be called from a function to redirect? I want the same effect that Link has where it doesn't fully re-render things. For that reason, useNavigate won't work as I've had issues with that before iirc.
Answered by larkx
const handleClick = (e: any, url: string) => {
  e.preventDefault()
  router.push(url)
}

Import useRouter from nezt/navigation and use that function to go to the page.
View full answer

1 Reply

Avatar
larkxOP
const handleClick = (e: any, url: string) => {
  e.preventDefault()
  router.push(url)
}

Import useRouter from nezt/navigation and use that function to go to the page.
Answer