<Button> inside <Link> of Next.js, refresh the page on navigation to another route.
NextUI Version
2.2.9
Describe the bug
<Link href="/auth/login?user_type=jobseeker">
<Button color="primary" type="button" radius="sm">
Login
</Button>
</Link>
- So basically what i wanted to do was wrap the
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
import Link from "next/link";
import { Button } from "@nextui-org/react";
function Component(){
<Link href="/home">
<Button color="primary">
Home
</Button>
</Link>
}
export default Component;
Expected behavior
Since, Next.js provide seamless routing, i didn't expect page to refresh when wrapping Next UI Button inside Link of next.js
Screenshots or Videos
No response
Operating System Version
Windows
Browser
Chrome
Try using this instead.
<Button
as={Link}
href="/auth/login?user_type=jobseeker"
color="primary"
type="button"
radius="sm"
>
Login
</Button>
Did you solve this? Having the same issue
Try using this instead.
<Button as={Link} href="/auth/login?user_type=jobseeker" color="primary" type="button" radius="sm" > Login </Button>
It worked for me.
Did you solve this? Having the same issue
yeah try the above-mentioned solution by @CaseyBlackburn
Closing - see the solution here.
Ehh I'm necroing this, still an issue specifically for other elements that you want to turn into Buttons.
For example, <Card as={Button} />:
export default function ArtistCard({ user }) {
return (
<Link
href={{
pathname: "/[username]",
query: { username: user.username},
}}
>
<Card as={Button} fullWidth className="px-2">
<CardBody className="gap-2">
<div className="flex items-center gap-4">
<UserAvatar user={user} size="lg" />
<div className="flex flex-col gap-0.5">
<p className="font-semibold">
{user.full_name || user.username}
</p>
<div className="flex gap-1.5 items-center text-small">
<MapPinIcon className="size-4 text-secondary" />
{user?.city}
</div>
{user?.shop && (
<div className="flex gap-1.5 items-center text-small">
<BuildingStorefrontIcon className="size-4 text-secondary" />
{user?.shop}
</div>
)}
</div>
<div className="ms-auto text-tiny flex flex-col gap-1 items-center">
<CalendarIcon className="size-6" />
<p>
<AutoTranslate tKey="next_available">
Next available:
</AutoTranslate>
</p>
<p className="font-bold -mt-1 text-success">
4/20/24
</p>
</div>
</div>
{user?.style && (
<>
<Divider className="mt-1" />
<div className="text-small flex gap-1">
<b>
<AutoTranslate tKey="style">
Style:
</AutoTranslate>
</b>
<span className="truncate">
{user?.style}
</span>
</div>
</>
)}
</CardBody>
</Card>
</Link>
)
}
Andddd here is my solution for anyone who runs into this.
button-link.jsx
import { Button } from '@nextui-org/react'
import Link from 'next/link'
import { forwardRef } from 'react'
const ButtonLink = forwardRef(({ buttonAs = Link, ...props }, ref) => {
return (
<Button ref={ref} as={buttonAs} {...props} />
)
})
export default ButtonLink
Usage:
<Card as={ButtonLink} href="/">
<CardBody>
This is my card that is a Button and a Link!
</CardBody>
</Card>
Another consideration is that if you do <Button as={Link} /> you can no longer use the Link's "as" property to rewrite the visible URL.
A fix for this is to extend Link to accept a linkAs parameter
link-as.jsx
import Link from 'next/link'
import { forwardRef } from 'react'
const LinkAs = forwardRef(({ linkAs, ...props }, ref) => {
return (
<Link ref={ref} as={linkAs} {...props} />
)
})
export default LinkAs
Total usage example:
<Card as={ButtonLink} buttonAs={LinkAs} href="/edit-profile?push=true" linkAs="/edit-profile">
<CardBody>
This is my card that is a Button and a Link to /edit-profile with the push parameter hidden in the URL!
</CardBody>
</Card>
Hi, I think this problem is back but it's because of the way nextj doesn't allow to have a as a child. Before the recommendation given above worked for me, but with the latest updates nextui it stopped working. Here is the button:
<Button
key="main-dashboard"
as={NextLink}
prefetch={false}
href="/dashboard"
className={classnames("rounded-sm bg-transparent data-[hover=true]:bg-zinc-200 text-sm", {
"bg-green-700 text-zinc-50 data-[hover=true]:bg-green-700": activeMainLink,
})}
size="sm"
variant="solid"
>
{ "Main"}
</Button>
As I said before it was working, but when upgrading nextui from 2.4 to 2.6, nextjs ( 14.2.18) started complaining that I'm using a as child.
nextjs version 14.2.18 nextui 2.6.4
this is the error message from log:
Error: Invalid <Link> with <a> child. Please remove <a> or use <Link legacyBehavior>.
Learn more: https://nextjs.org/docs/messages/invalid-new-link-with-extra-anchor
as can take a function that returns a component... for example
as={(props) => <NextLink legacyBehavior {...props} />}
Will that fix it?
So I'm not having any problems with this in NextUI 2.6.4
<Button
as={NextLink}
href="/settings?tab=notifications"
variant="light"
onPress={() => setIsOpen(false)}
>
Settings
</Button>
Only difference is I'm on Next 15
Hi @daveycodez , solved Button with Link using passHref props. But Links inside DropdownItem i cant solved with this
Yea I had to move all of my DropdownItem links over to onPress with router push while they are working on that, I made a separate issue for the Dropdown links
Thx for the tip, now using router push too.
what now? in heroui 3?