New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Next 9 - Using functional components as child of <Link/> causes ref-warnings #7915
Comments
Same here. I am having error If I use custom component like this: <Link href="/href">
<Button
type="custom"
color="#FCFCFC"
>
buttonText
</Button>
</Link> This is only happening when you use a component as a child. These don't give a warning for example: <Link href="/href">
<button
type="custom"
color="#FCFCFC"
>
buttonText
</button>
</Link>
<Link href="/href">
<a>
<Button
type="custom"
color="#FCFCFC"
>
buttonText
</Button>
</a>
</Link> |
Is there a way around this bug? we are blocked from going to 9 for this one when some features we would really like to leverage like the typescript support. Just noticed it bumped to 9.0.3 :/ |
You could try muting it temporarily , like: // TODO: Muting error, fix as soon as zeit/next.js/issues/7915 resolved
const originalError = console.error;
console.error = (...args) => {
if (/Warning.*Function components cannot be given refs/.test(args[0])) {
return;
}
originalError.call(console, ...args);
}; |
I'm having the same issue here. |
If you use const CustomComponent = React.forwardRef(function CustomComponent(props, ref) {
return (
<div/>
);
}); <Link href={"/"}>
<CustomComponent/>
</Link> The warning goes away. |
I have updated to
|
@th0th You should wrap your component in import React from 'react'
import Link from 'next/link'
const CustomComponent = React.forwardRef((props, ref) => (
<a ref={ref} {...props}>
Click
</a>
))
export default () => (
<Link href='/' passHref>
<CustomComponent/>
</Link>
) |
Wrap all functional components used within a |
Agree with @nickluger this seems counter-intuitive. |
Should all other libraries implement |
Yes agree with @nickluger, it's a pain to do this manually for all the components inside a |
Should we reopen and rename this issue (@ijjk) or did someone already open a new one? |
This will most likely throw a console error:
|
I just hit this issue when upgrading Next JS. Is there a fix on the way? I'm not sure I understand how to use forwardRef and wrap my component. My button.js file currently exports the following....
|
See above, I opened a follow-up here: #8962 |
I'm just creating a wrapper component for generic usage:
and then I'm using this inside the Link like the following:
|
import NextLink from "next/link";
function LinkWrap({ children, refAs, ...props }, ref) {
if (refAs) {
props[refAs] = ref;
}
return (
<>
{React.isValidElement(children)
? React.cloneElement(children, props)
: null}
</>
);
}
const LinkWrapper = React.forwardRef(LinkWrap);
function Link({ refAs, children, ...props }) {
return (
<NextLink {...props}>
<LinkWrapper refAs={refAs}>{children}</LinkWrapper>
</NextLink>
);
} and use created |
* chore(clean-nextjs): fix boot errors * feat(title): add page titles * fix(bugfix-react-select): add instanceId everywhere * fix(bugfix-ssr): fix ssr loading text conflict * fix(fix-nextlink-ref): vercel/next.js/issues/7915#issuecomment-747433561
This comment has been minimized.
This comment has been minimized.
@machineghost Be nice - there are human people on the receiving end of your words. |
If we're keen to use our own function component directly within Next's import { Button } from 'some-package';
const ButtonNext = React.forwardRef(({ children, ...rest }, ref) => (
<span ref={ref}>
<Button {...rest}>{children}</Button>
</span>
));
// ...
<Link href="/" passHref>
<ButtonNext>Go home</ButtonNext>
</Link> We can't attach a |
Why is this issue closed? Was it solved? If so, where? |
I have a next <Image .../> component an a and it throw the same warning. I put a div inside Link and it works but yes, it seems hacky |
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. |
This is very much a riddle, do I spend time "fixing" this warning even though it's not a functional issue? |
The link that @cgarrovillo provided fixed the issue for me. The warnings went away and my custom functional component is working well with the link. https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-function-component |
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 |
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 |
What's the use of forwarding the Can I simply have it like this? |
+1 for this issue being re-opened. In the majority of cases Anyway we can contact the core team so Link is usable again? |
If I wrap the functional component in
|
So I followed this https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-custom-component-that-wraps-an-a-tag
With this I have my component wrapped in |
yep this worked for me too! |
This might help others, this is how I've solved with TS. It does feel like a big dirty hack having to add this import Link from 'next/link';
import { forwardRef } from 'react';
export interface MediaCardProps extends CardProps {
headline: string;
children: string;
href: string;
}
type MediaCardPropsWithOptionalHref = Omit<MediaCardProps, 'href'> & {
href?: string;
};
const CardWrapped = forwardRef<any, MediaCardPropsWithOptionalHref>(
({ children, headline, href }, ref) => (
<div ref={ref}>
<Card layout="vertical" href={href}>
{/* redacted */}
</Card>
</div>
)
);
export const MediaCard = ({ href, children, headline }: MediaCardProps) => (
<Link href={href} passHref>
<CardWrapped headline={headline}>{children}</CardWrapped>
</Link>
);
|
This is a small pain in the butt every time you need to use a |
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. |
Bug report
Describe the bug
Using a functional component as a child of
<Link/>
causes: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
The text was updated successfully, but these errors were encountered: