Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an href to an onClick function in nextjs?

In the following code

import Link from "next/link";

export default function IndexPage() {
  const handleClick = (path) => {
    if (path === "/about") {
      console.log("I clicked on the About Page");
    }
    if (path === "/posts") {
      console.log("I clicked on the Posts Page");
    }
  };
  return (
    <div>
      Hello World.{" "}
      <Link onClick={() => handleClick("/about")} href="/about">
        <a>About</a>
      </Link>
      <Link onClick={() => handleClick("/posts")} href="/posts">
        <a>Posts</a>
      </Link>
    </div>
  );

Whenever the about or posts page is clicked, I'd like to console.log that I clicked on the page name. Right now, my current implementation does not console.log anything. What is wrong with my code?

like image 274
dev_el Avatar asked Jan 09 '21 02:01

dev_el


People also ask

How do I use onclick on Nextjs?

Show activity on this post. import Link from "next/link"; export default function IndexPage() { const handleClick = (path) => { if (path === "/about") { console. log("I clicked on the About Page"); } if (path === "/posts") { console. log("I clicked on the Posts Page"); } }; return ( <div> Hello World.

How do I link a component in Nextjs?

When linking between pages on websites, you use the <a> HTML tag. In Next. js, you can use the Link Component next/link to link between pages in your application. <Link> allows you to do client-side navigation and accepts props that give you better control over the navigation behavior.

How do I add a external link in Nextjs?

To add an external link to the Next. js Link component we should use attribute passHref. This attribute is set to false by default. This attribute forces Link to send the href property to its child.

What does passHref do in Nextjs?

passHref - Forces Link to send the href property to its child. Defaults to false. prefetch - Prefetch the page in the background.


Video Answer


2 Answers

You can move the onClick handler to the <a> tag.

import { useRouter } from "next/router";
import Link from "next/link";

export default function IndexPage() {
  const router = useRouter();

  const handleClick = (e, path) => {
    if (path === "/about") {
      console.log("I clicked on the About Page");
    }
    if (path === "/posts") {
      console.log("I clicked on the Posts Page");
    }
  };
  return (
    <div>
      Hello World.{" "}
      <Link href="/">
        <a onClick={(e) => handleClick(e, "/about")}>About</a>
      </Link>{" "}
      <Link href="/">
        <a onClick={(e) => handleClick(e, "/posts")}>Posts</a>
      </Link>
    </div>
  );
}
like image 173
user9052491 Avatar answered Nov 15 '22 04:11

user9052491


updating answer to handle race condition between href and onClick

Since you are redirected when clicking the <Link>, I wouldn't try to manage both href and onClick.

I would do everything on the handleClick function, even redirecting the user in a programatically way:

import { useRouter } from 'next/router'

export default function IndexPage() {
  const router = useRouter()

  const handleClick = (e, path) => {
   e.preventDefault()

    if (path === "/about") {
      console.log("I clicked on the About Page");
      // then you can: 
      // router.push(path)
    }
    if (path === "/posts") {
      console.log("I clicked on the Posts Page");
      // then you can: 
      // router.push(path)
    }
  };
}

  return (
    <div>
      Hello World.{" "}
      <Link onClick={(e) => handleClick(e, "/about")}>
        <a>About</a>
      </Link>
      <Link onClick={(e) => handleClick(e, "/posts")}>
        <a>Posts</a>
      </Link>
    </div>
  );

Note that if you do router.push right after the console.log, the console.log would not be shown, since you are being redirected. But I believe you want to do some extra work before pushing the user, so you can follow this approach anyway.

like image 32
yaiks Avatar answered Nov 15 '22 05:11

yaiks