Link with forwardRef component not working
See original GitHub issueBug report
Describe the bug
Link with forward ref doesn’t work anymore. If I pass passHref than it passes the href but the page gets reloaded.
To Reproduce
Steps to reproduce the behavior, please provide code snippets or a repository:
import React from "react";
import Link from "next/link";
const MyButton = React.forwardRef(({ onClick, href }, ref) => (
<a href={href} onClick={console.log} ref={ref}>
Click Me
</a>
))
const Home = () => (
<div>
<Link href="/another">
<MyButton />
</Link>
</div>
);
export default Home;
https://codesandbox.io/s/nextjs-wtnzg https://github.com/rahul3103/testLink
Expected behavior
I expected url to be seen in the bottom left on hover but it was not there, than I inspected a and there was no href. But, I was getting the onClick event. Than, I added passHref to Link. After that href was there but onClick the whole page was getting re rendered.
System information
- OS: [macOS]
- Browser [chrome]
- Version of Next.js: [9.1.6]
Additional context
Add any other context about the problem here.
Issue Analytics
- State:
- Created 4 years ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
NextJS > Function components cannot be given refs. Attempts ...
I'm trying to wrap a card component using the Link component from 'next/link'. When I click the card it should have taken me...
Read more >Forwarding Refs - React
Ref forwarding is a technique for automatically passing a ref through a component to one of its children. This is typically not necessary...
Read more >warning: function components cannot be given refs. attempts ...
As the warning state you cannot assign refs to functional component without the usage the forwardRef. In order to have access to refs...
Read more >Composition - Material UI - MUI
To solve this problem, we tag some of the components with a muiName static ... forwardRef<HTMLAnchorElement, Omit<RouterLinkProps, 'to'>>(function Link( ...
Read more >How to use React's forwardRef function | Felix Gerschau
By default, the ref prop only works on HTML elements, not on React components. When we want to pass down a reference to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

As mentioned by @jamesmosier the props like
onClickadded bynext/linkneeds to be passed or else Next.js can’t handle it for you. We recommend having theatag directly as the child of aLinkcomponent since it provides the best experience and makes sure you don’t have to worry about passing props like this.I’m going to close this as it doesn’t seem to be an issue with Next.js and we also already have tests covering this here
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.