[BUG] - Clicking a Tab link causes a full page reload when using Remix
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
- Browse to the
/exampleroute - Click one of the tabs
- Observe the underline disappears and then appears under the selected tab
- 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
Same with NextJs
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.
Same issue with Next.js
Adding another +1 for visibility. With NextJS using App Router (following the useRouter/Next Navigation instructions).
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
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" />
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();
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
+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
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)
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.
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>
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.....
I've fixed it in Next.js by importing
Linkand using theasproperty 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!...
Fixed in https://github.com/nextui-org/nextui/pull/3240
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>
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