nextui icon indicating copy to clipboard operation
nextui copied to clipboard

<Button> inside <Link> of Next.js, refresh the page on navigation to another route.

Open sarozpradhan64 opened this issue 3 years ago • 14 comments

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

sarozpradhan64 avatar Nov 18 '23 14:11 sarozpradhan64

Try using this instead.

<Button
    as={Link}
    href="/auth/login?user_type=jobseeker"
    color="primary"
    type="button"
    radius="sm"
>
    Login
</Button>

CaseyBlackburn avatar Nov 22 '23 02:11 CaseyBlackburn

Did you solve this? Having the same issue

vpaul18 avatar Nov 27 '23 17:11 vpaul18

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.

sarozpradhan64 avatar Dec 02 '23 06:12 sarozpradhan64

Did you solve this? Having the same issue

yeah try the above-mentioned solution by @CaseyBlackburn

sarozpradhan64 avatar Dec 02 '23 10:12 sarozpradhan64

Closing - see the solution here.

wingkwong avatar Apr 05 '24 06:04 wingkwong

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>
    )
}

daveycodez avatar Jul 26 '24 06:07 daveycodez

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>

daveycodez avatar Jul 26 '24 06:07 daveycodez

as can take a function that returns a component... for example

as={(props) => <NextLink legacyBehavior {...props} />}

Will that fix it?

daveycodez avatar Dec 07 '24 19:12 daveycodez

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

daveycodez avatar Dec 07 '24 19:12 daveycodez

Hi @daveycodez , solved Button with Link using passHref props. But Links inside DropdownItem i cant solved with this

BorisBarzotto avatar Dec 07 '24 20:12 BorisBarzotto

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

daveycodez avatar Dec 07 '24 20:12 daveycodez

Thx for the tip, now using router push too.

BorisBarzotto avatar Dec 07 '24 20:12 BorisBarzotto

what now? in heroui 3?

krychaxp avatar Jun 22 '26 14:06 krychaxp