nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[BUG] - Clicking a Tab link causes a full page reload when using Remix

Open corydeppen opened this issue 3 years ago • 17 comments

NextUI Version

2.2.2

Describe the bug

Tab links don't appear to work properly with Remix. As you can see in the example, the selected tab is eventually underlined, but it's not a smooth transition because the full page is reloaded instead of using client side routing.

Your Example Website or App

https://stackblitz.com/edit/remix-run-remix-6ogybb?file=app%2Froutes%2Fexample.tsx

Steps to Reproduce the Bug or Issue

  1. Browse to the /example route
  2. Click one of the tabs
  3. Observe the underline disappears and then appears under the selected tab
  4. Inspecting the network tab in the browser DevTools will show the full page is being reloaded on each tab navigation

Expected behavior

Client side routing should be used when clicking each tab - the full page should not reload for each navigation. The underline should transition to the selected tab as it does when links are not used.

Screenshots or Videos

No response

Operating System Version

macOS

Browser

Chrome

corydeppen avatar Nov 05 '23 04:11 corydeppen

Same with NextJs

ynn1k avatar Nov 05 '23 14:11 ynn1k

Just another +1 for this happening in NextJS (App Router) as well. And this is with router.push from useRouter in Next Navigation being set in the NextUI Provider.

CaseyBlackburn avatar Nov 08 '23 00:11 CaseyBlackburn

Same issue with Next.js

MeonisRP avatar Nov 09 '23 12:11 MeonisRP

Adding another +1 for visibility. With NextJS using App Router (following the useRouter/Next Navigation instructions).

avillaindesire avatar Nov 22 '23 17:11 avillaindesire

Same here , +1, NextJS, App Router following the docs here and here --> with useRouter/Next Navigation instructions.

Also For behavioural purposes of accessing the selected tab from other pages, but not using usePathname() (as it was giving me 404), I opted for useSearchParams()

  • on 'home' or any other page setting the Link href={/oferta?tab=${item.slug}}
  • then in the component where your Tabs is const searchParams = useSearchParams() const tab: any = searchParams.get('tab') const [selected, setSelected] = useState(tab);
  • lastly in Tabs selectedKey={selected} onSelectionChange={setSelected}
  • and we don't want to pass the href in Tab as to not cause the mentioned reload

It's not the solution as this is not synchronizing the selected tab with the current URL from your client side router, but it suits my needs. I haven't test it in production mode, only in dev. Hope it comes in handy <3

NanaNiki avatar Nov 22 '23 21:11 NanaNiki

I managed to add a smooth transition by setting up as prop for each tab component. To be honest, I can't understand the benefit of the href prop that the tab component provides. In my opinion, this prop shouldn't exist.

https://stackblitz.com/edit/remix-run-remix-nuqeei?file=app%2Froutes%2Fexample.tsx

<Tab title="Notes" as={RemixLink} to="/example/notes" key="/example/notes" />

sdreva avatar Nov 24 '23 06:11 sdreva

I have managed it like this in my Next js App and it's working fine . I don;t know it's correct way or not because with default href with tab it's refreshing the page .

 <Tabs
        size="md"
        fullWidth={true}
        color="primary"
        className="mt-2"
        selectedKey={pathName}
        onSelectionChange={(value: any) => {
          console.log("The value is", value);
          router.push(value);
        }}
      >
        <Tab key="/" title="Posts">
          <DisplayPosts postParam={postRes} session={session} />
        </Tab>
        <Tab key="/community/post" title="Community Posts">
          <CommunityPosts session={session} postParam={communityPosts} />
        </Tab>
      </Tabs>

And I have initialised router and pathname like this

  const pathName = usePathname();
  const router = useRouter();

TusharPayoMatix avatar Dec 13 '23 17:12 TusharPayoMatix

I have managed it like this in my Next js App and it's working fine . I don;t know it's correct way or not because with default href with tab it's refreshing the page .

 <Tabs
        size="md"
        fullWidth={true}
        color="primary"
        className="mt-2"
        selectedKey={pathName}
        onSelectionChange={(value: any) => {
          console.log("The value is", value);
          router.push(value);
        }}
      >
        <Tab key="/" title="Posts">
          <DisplayPosts postParam={postRes} session={session} />
        </Tab>
        <Tab key="/community/post" title="Community Posts">
          <CommunityPosts session={session} postParam={communityPosts} />
        </Tab>
      </Tabs>

And I have initialised router and pathname like this

  const pathName = usePathname();
  const router = useRouter();

With this page is not reloading

TusharPayoMatix avatar Dec 13 '23 17:12 TusharPayoMatix

+1 for NextJs not working, the implementation in the docs leads to reloads and navigation to the respective href rather than sync to the existing path

casualzach avatar Dec 21 '23 15:12 casualzach

work around for nextJS i've found to work is by not using the href in the Tab but adding a Link item to the title like this:

 <Tabs
        aria-label="Options"
        selectedKey={pathname}
        defaultSelectedKey={`/${id}/work`}
      >
        <Tab
          key={`/${id}/work`}
          title={<Link href={`${path}/work`}>Work</Link>}
        />
        <Tab
          key={`/${id}/about`}
          title={<Link href={`${path}/about`}>About</Link>}
        />

        <Tab
          key={`/${id}/collections`}
          title={<Link href={`${path}/collections`}>Collections</Link>}
        />
      </Tabs>

This requires the routes to be set up as seen in the parallel routes examples in the offical docs (an easier but less clean alternative to this is to just use url Params, or if folder structure is needed to set up the route like /[id]/[tab]/page.tsx and use the tab as an initial parameter together with useState history push)

casualzach avatar Dec 22 '23 11:12 casualzach

work around for nextJS i've found to work is by not using the href in the Tab but adding a Link item to the title like this:

 <Tabs
        aria-label="Options"
        selectedKey={pathname}
        defaultSelectedKey={`/${id}/work`}
      >
        <Tab
          key={`/${id}/work`}
          title={<Link href={`${path}/work`}>Work</Link>}
        />
        <Tab
          key={`/${id}/about`}
          title={<Link href={`${path}/about`}>About</Link>}
        />

        <Tab
          key={`/${id}/collections`}
          title={<Link href={`${path}/collections`}>Collections</Link>}
        />
      </Tabs>

This requires the routes to be set up as seen in the parallel routes examples in the offical docs (an easier but less clean alternative to this is to just use url Params, or if folder structure is needed to set up the route like /[id]/[tab]/page.tsx and use the tab as an initial parameter together with useState history push)

Hi, Thank you for your helping code. I confirm that it is working well, but the keyboard interactions are not functioning on the tabs.

masanori-iwata avatar Dec 28 '23 03:12 masanori-iwata

I managed to add a smooth transition by setting up as prop for each tab component. To be honest, I can't understand the benefit of the href prop that the tab component provides. In my opinion, this prop shouldn't exist.

https://stackblitz.com/edit/remix-run-remix-nuqeei?file=app%2Froutes%2Fexample.tsx

<Tab title="Notes" as={RemixLink} to="/example/notes" key="/example/notes" />

That worked for me in NextJS. I just replaced "to" with "href" and "RemixLink" with "Link":

<Tab key="/about" title="Profile" as={Link} href="/about" >...</Tab>

flnx avatar Jan 07 '24 15:01 flnx

I've fixed it in Next.js by importing Link and using the as property as following:

import Link from "next/link";

...

<Tab as={Link} key="about" title="About Us" href="/about"></Tab>

Anything wrong with that I'm unaware of? This isn't the first time the official NextUI docs give me a hard time, I mean really wasting me hours.....

edxeth avatar Jan 16 '24 16:01 edxeth

I've fixed it in Next.js by importing Link and using the as property as following:

import Link from "next/link";

...

<Tab as={Link} key="about" title="About Us" href="/about"></Tab>

Anything wrong with that I'm unaware of? This isn't the first time the official NextUI docs give me a hard time, I mean really wasting me hours.....

It does somewhat work but it's not a true fix, Animations are not present!...

VenkatLohithDasari avatar Mar 20 '24 11:03 VenkatLohithDasari

Fixed in https://github.com/nextui-org/nextui/pull/3240

wingkwong avatar Jun 15 '24 07:06 wingkwong

this still seems to be an issue with Remix in the latest version 2.4.6.

Getting around it with <Tab as={Link} key="/path" title="Title" to="/path" /> but "to" throws a type error:

TS2322: Type 
{     
    as: ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>>;     
    key: string;     
    title: string;     
    to: string; 
} 
is not assignable to type IntrinsicAttributes & TabItemProps<object>
Property to does not exist on type IntrinsicAttributes & TabItemProps<object>

kisonay avatar Aug 24 '24 15:08 kisonay

this still seems to be an issue with Remix in the latest version 2.4.6.

Getting around it with <Tab as={Link} key="/path" title="Title" to="/path" /> but "to" throws a type error:

TS2322: Type 
{     
    as: ForwardRefExoticComponent<RemixLinkProps & RefAttributes<HTMLAnchorElement>>;     
    key: string;     
    title: string;     
    to: string; 
} 
is not assignable to type IntrinsicAttributes & TabItemProps<object>
Property to does not exist on type IntrinsicAttributes & TabItemProps<object>

some here

zhixinpeng avatar Sep 14 '24 02:09 zhixinpeng