question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Link with forwardRef component not working

See original GitHub issue

Bug 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:closed
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ijjkcommented, Dec 19, 2019

As mentioned by @jamesmosier the props like onClick added by next/link needs to be passed or else Next.js can’t handle it for you. We recommend having the a tag directly as the child of a Link component 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

0reactions
balazsorban44commented, Jan 30, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found