next.js: Next 9 - Using functional components as child of causes ref-warnings
Bug report
Describe the bug
Using a functional component as a child of <Link/> causes:
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
This happened only after upgrading from v8 => v9.
To Reproduce
Reproduction repo: https://github.com/nickluger/nextjs-link-ref-functional-comp-warning
Expected behavior
<Link/> should work with functional components, too / not show a warning.
System information
- Version of Next.js: 9.0.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 121
- Comments: 57 (6 by maintainers)
Commits related to this issue
- fix NextLink + React.forwardRef https://github.com/zeit/next.js/issues/7915#issuecomment-519786376 — committed to trezor/trezor-suite by szymonlesisz 7 years ago
- fix NextLink + React.forwardRef https://github.com/zeit/next.js/issues/7915#issuecomment-519786376 — committed to zuzjes/trezor-suite by szymonlesisz 7 years ago
- Fix React warning https://github.com/zeit/next.js/issues/7915 — committed to innodoc/innodoc-webapp by tumidi 7 years ago
- Fix React warning https://github.com/zeit/next.js/issues/7915 — committed to innodoc/innodoc-webapp by tumidi 7 years ago
- fix(fix-nextlink-ref): vercel/next.js/issues/7915#issuecomment-747433561 — committed to SocialGouv/emjpm by devthejo 6 years ago
- feat(nextjs-enhancements): add pages title + fix nextjs warnings (#2383) * chore(clean-nextjs): fix boot errors * feat(title): add page titles * fix(bugfix-react-select): add instanceId everywh... — committed to SocialGouv/emjpm by devthejo 6 years ago
- fix: bug of https://github.com/vercel/next.js/issues/7915 — committed to takakimiyajima/my-weight by takakimiyajima 5 years ago
You could try muting it temporarily , like:
Wrap all functional components used within a
Linkmanually? Shouldn’t there be a easier, less repetitive solution for this? At least something like<Link href="/" forwardRef>...</Link>?Same here. I am having error If I use custom component like this:
This is only happening when you use a component as a child. These don’t give a warning for example:
I was watching the NextJS conference the other day thinking “Wow Next is the future”. I really like Next and the Vercel ecosystem and I want to honestly be able to say that and be an advocate for Next. However, issues like these being closed and not addressed make it hard to do so. I’ve seen this type of pattern multiple times throughout the issues here, specifically with the Router.
There’s 3 solutions to this problem from the user side and none of them are great DX.
React.forwardRef. This can be very repetitivediv,span,a, etc.router.pushNo matter which solution you use, they’re all strange and require comments linking to this issue or the docs to explain to coworkers/contributors why its needed to prevent accidental removal/cleanup.
I think this issue should be reopened for discussion.
Following @hill84’s suggestion was a nice workaround:
If you use
<Link/>to wrap your custom component, you could forwardreflike this:The warning goes away.
@machineghost Be nice - there are human people on the receiving end of your words.
Warning does go away when using react fragment, but in tat case link doesn’t work for me
I recently came across this issue and the way i solved is like this:
So React Fragment seems to work in this case.
Why is this ref even needed? Is it to guarantee the correct placement of the
hrefin the underlying wrapped component whenpassHrefis used in the<Link />?This is super annoying as my custom <Button /> component has the
<a>at the root and I can see it’s getting href applied to it correctly anyway… I’d much rather have it enforced that you need your underlying custom component to have the anchor at the root and avoid this wholerefdesign desicion.React.forwardRefis a horribly cumbersome thing to force people to implement and is playing hell with my linters 😞Yes agree with @nickluger, it’s a pain to do this manually for all the components inside a
Link.Why is this issue closed? Was it solved? If so, where? I solved it with the <div></div> trick, but still seems hacky.
@th0th You should wrap your component in
React.forwardRef. Like this one:I use
<><Component/></>but Link can not action. I have to use<div></div>instead of.This is a small pain in the butt every time you need to use a
Link. Most components are functional components nowadays. Wrapping in<a>seems to be the most straightforward solution for most cases. I really think the nextjs team should think of a more convenient API though.Works for me aswell. Wondering if it has any implications…
+1 for this issue being re-opened. In the majority of cases
Linkis used to wrap a functional component, and usingforwardRefin each component is not practical. Is there a reason the core team isn’t open to implementing one of the suggested solutions?Anyway we can contact the core team so Link is usable again?
Didn’t realize there are some issues like this lurking in NextJS. And by issue, I mean how the primary concern in this matter is seen as nothing to care about.
and use created
Linkcomponent instead of next.js oneI suggest a solution where a function returns an object with props instead of a component :
Checked, it works!
If I wrap the functional component in
<a></a>it works fine with no forwardRef… and I’ve been using this method for years - does this break proper internal routing in some way I’m not seeing?<Link href="/internal-link"><a><MyFunctionalComponent/></a></Link>I’ve just encountered this error today! Problem fixed with this guide https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-function-component
EDITED: This solution is simultaneously genius AND dumb. Genius because progammer-rkt has found a clever and minimal solution to the problem … but dumb because that genius solution shouldn’t have been necessary in the first place.
Maybe this comes across as mean, but I truly think any competent React dev would understand that when you make a component for others to use, it shouldn’t be written it in a way that requires its children to be wrapped in meaningless fragments.
Next version: 10.0.1. The problem still persists.
Should we reopen and rename this issue (@ijjk) or did someone already open a new one?
This will most likely throw a console error:
Warning: validateDOMNesting(...): <a> cannot appear as a descendant of <a>.@progammer-rkt @huntedman @maksimf Just keep in mind that
passHrefwont work with this solution…https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-function-component
What about this solution using a react fragment as a bearing wrapper?
Where
<MyFunctionalComponent>forwards the ref:This doesn’t add any additional HTML wrapper in the output (
<div />) and should not trigger any related error.yep this worked for me too!
Wrapping it in a div tag worked, but it didn’t render the Links anchor tag which could mess up SEO. Instead, I made a custom Link wrapper which wraps the component in an anchor tag- as described here. https://jasonwatmore.com/post/2021/05/31/next-js-make-the-link-component-work-like-react-router-link
@Vadorequest I’ve added more details to my previous comment.