-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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/link doesn't work when child is function component #8425
Comments
Guess your button component should simply relay -const Button = ({ children }) => (
- <a className="button">
+const Button = ({ children, href }) => (
+ <a className="button" href={href}>
{children}
</a>
); It might be a good idea to pass |
Hi, the syntax to use import React from "react";
import Link from "next/link";
const Button = React.forwardRef(({ children, href, onClick }, ref) => (
<a
ref={ref}
href={href}
onClick={onClick}
className="button"
>
{children}
</a>
));
const TestThing = () => (
<Link href="/" passHref>
<Button>Checkout</Button>
</Link>
);
export default TestThing; For more information on |
@ijjk Thank you but I can confirm this is not working. Same problem where it creates the anchor with the correct href but clicking it does a SSR rather than client side route change. Since I am correctly applying the forwardRef and the link correctly does client side routing when using an anchor tag directly instead of a function component child, I can only assume this is a NextJS bug at the moment. Are we able to re-open this issue? |
@caribou-code can you provide a link to a reproduction? We have tests covering |
@ijjk Ok I've discovered I was missing the onClick prop in my Button component and that's what was breaking it (I realise now you had that in your example above). I just assumed I didn't need it because I'm not using click events or |
For this routing to work without refresh how does your pages look lilke? |
I was dealing with this issue exactly--and this solved my problem! Now I'm wondering why Next.js wants navigation links to be written like this? |
This seems like a really big issue. |
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
When using next/link and passing a function component as the child, it does a full server side render and page refresh on click rather than a client side routing.
To Reproduce
The following link works:
The following does a SSR load rather than client side on click:
Additional context
I've seen a new addition to the README in v9.0.4-canary.2:
Note: if passing a functional component as a child of <Link> you will need to wrap it in React.forwardRef
I've tried various methods of adding a ref including forwardRef on my Button component but nothing seems to work. Perhaps I'm doing it wrong. Can someone confirm whether this is a bug with the link component or just provide an example of how this should look with the correct ref setup? I feel like the docs are not clear on this.
The text was updated successfully, but these errors were encountered: