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?
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.
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.
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.
passHref - Forces Link to send the href property to its child. Defaults to false. prefetch - Prefetch the page in the background.
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>
);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With