[BUG] - Tabs component rendering error in ssr state
NextUI Version
"@nextui-org/tabs": "^2.0.37"
Describe the bug
I am using the official template project of NextUI for development, and I am interested in customizing the navbar effect, so I looked into the switching effect of tabs. When I use tabs like this,
import { Tabs, Tab } from "@nextui-org/tabs";
import { Icon } from "@iconify/react";
import { siteConfig } from "@/config/site";
export interface NavMenuProps {}
export default function NavMenu({}: NavMenuProps) {
return (
<Tabs isVertical aria-label="Navigation" color="primary" variant="bordered">
{siteConfig.navItems.map((item) => (
<Tab
key={item.href}
title={
<div className="flex items-center space-x-2">
<Icon icon={item.icon} />
<span>{item.label}</span>
</div>
}
/>
))}
</Tabs>
);
}
报错信息如下
When I add "use client", the component renders correctly
Your Example Website or App
No response
Steps to Reproduce the Bug or Issue
。
Expected behavior
ssr state can also render normally
Screenshots or Videos
No response
Operating System Version
Windows
Browser
Edge
Seems to be related to this https://github.com/nextui-org/nextui/issues/1403#issuecomment-1678863519
An example
import { Tab, Tabs } from '@nextui-org/tabs'
import { IntroductionTab } from '~/components/patch/introduction/IntroductionTab'
import { ResourceTab } from '~/components/patch/resource/ResourceTab'
import { CommentTab } from '~/components/patch/comment/CommentTab'
import type { PatchIntroduction } from '~/types/api/patch'
import type { Dispatch, SetStateAction } from 'react'
interface PatchHeaderProps {
id: number
intro: PatchIntroduction
selected: string
setSelected: Dispatch<SetStateAction<string>>
}
export const PatchHeaderTabs = ({
id,
intro,
selected,
setSelected
}: PatchHeaderProps) => {
return (
<Tabs
className="w-full my-6 overflow-hidden shadow-medium rounded-large"
fullWidth={true}
defaultSelectedKey="introduction"
onSelectionChange={(value) => {
if (value === 'resources') {
window.scroll(0, 400)
}
setSelected(value.toString())
}}
selectedKey={selected}
>
<Tab key="introduction" title="Introduction" className="p-0">
<IntroductionTab intro={intro} patchId={Number(id)} />
</Tab>
<Tab key="resources" title="Resources" className="p-0">
<ResourceTab id={id} />
</Tab>
<Tab key="comments" title="Comments" className="p-0">
<CommentTab id={id} />
</Tab>
</Tabs>
)
}
It works fine in my website.
Import Tabs from '@nextui-org/tabs', not '@nextui-org/react'
Another example
'use client'
import { Card, CardBody } from '@nextui-org/card'
import { Tab, Tabs } from '@nextui-org/tabs'
import { usePathname } from 'next/navigation'
import Link from 'next/link'
interface UserActivityProps {
id: number
}
export const UserActivity = ({ id }: UserActivityProps) => {
const pathname = usePathname()
const lastSegment = pathname.split('/').filter(Boolean).pop()
const tabs = [
{ key: 'comment', title: 'Comments', href: `/user/${id}/comment` },
{ key: 'favorite', title: 'Favorites, href: `/user/${id}/favorite` },
{ key: 'resource', title: 'Eesources', href: `/user/${id}/resource` }
]
return (
<Card className="w-full">
<CardBody>
<Tabs
aria-label="Activity"
variant="underlined"
fullWidth
selectedKey={lastSegment}
>
{tabs.map(({ key, title, href }) => (
<Tab key={key} as={Link} title={title} href={href} />
))}
</Tabs>
</CardBody>
</Card>
)
}
Maybe this comment can help you to solve your problem. https://github.com/frontio-ai/heroui/issues/1403#issuecomment-2461598220
An example示例
import { Tab, Tabs } from '@nextui-org/tabs' import { IntroductionTab } from '~/components/patch/introduction/IntroductionTab' import { ResourceTab } from '~/components/patch/resource/ResourceTab' import { CommentTab } from '~/components/patch/comment/CommentTab' import type { PatchIntroduction } from '~/types/api/patch' import type { Dispatch, SetStateAction } from 'react'
interface PatchHeaderProps { id: number intro: PatchIntroduction selected: string setSelected: Dispatch<SetStateAction
> } export const PatchHeaderTabs = ({ id, intro, selected, setSelected }: PatchHeaderProps) => { return ( <Tabs className="w-full my-6 overflow-hidden shadow-medium rounded-large" fullWidth={true} defaultSelectedKey="introduction" onSelectionChange={(value) => { if (value === 'resources') { window.scroll(0, 400) } setSelected(value.toString()) }} selectedKey={selected} >
<Tab key="resources" title="Resources" className="p-0"> <ResourceTab id={id} /> </Tab> <Tab key="comments" title="Comments" className="p-0"> <CommentTab id={id} /> </Tab> </Tabs>) } It works fine in my website.它在我的网站中运行良好。 Import Tabs from
'@nextui-org/tabs', not Import Tabs from (导入选项卡) ,而不是'@nextui-org/react'Another example另一个例子
'use client'
import { Card, CardBody } from '@nextui-org/card' import { Tab, Tabs } from '@nextui-org/tabs' import { usePathname } from 'next/navigation' import Link from 'next/link'
interface UserActivityProps { id: number }
export const UserActivity = ({ id }: UserActivityProps) => { const pathname = usePathname() const lastSegment = pathname.split('/').filter(Boolean).pop()
const tabs = [ { key: 'comment', title: 'Comments', href:
/user/${id}/comment}, { key: 'favorite', title: 'Favorites, href:/user/${id}/favorite}, { key: 'resource', title: 'Eesources', href:/user/${id}/resource} ]return (
) } Maybe this comment can help you to solve your problem.也许这条评论可以帮助你解决你的问题。 #1403 (comment)#1403 (如何) {tabs.map(({ key, title, href }) => ( ))}
Thank you for your reply, and I hope to merge into the global package soon.