You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJS报错Unsupported Server Component type: undefined的原因及解决

问题:Next.js添加'use client'后出现"Error: Unsupported Server Component type: undefined"

我在编写Next.js项目时,给NavLinks组件添加'use client'指令后,出现错误提示:

Error: Unsupported Server Component type: undefined

涉及代码如下:

'use client';
 
import {
  UserGroupIcon,
  HomeIcon,
  DocumentDuplicateIcon,
} from '@heroicons/react/24/outline';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import clsx from 'clsx';
 
// ...
 
export default function NavLinks() {
  const pathname = usePathname();
 
  return (
    <>
      {links.map((link) => {
        const LinkIcon = link.icon;
        return (
          <Link
            key={link.name}
            href={link.href}
            className={clsx(
              'flex h-[48px] grow items-center justify-center gap-2 rounded-md bg-gray-50 p-3 text-sm font-medium hover:bg-sky-100 hover:text-blue-600 md:flex-none md:justify-start md:p-2 md:px-3',
              {
                'bg-sky-100 text-blue-600': pathname === link.href,
              },
            )}
          >
            <LinkIcon className="w-6" />
            <p className="hidden md:block">{link.name}</p>
          </Link>
        );
      })}
    </>
  );
}

错误原因

  • 核心问题是代码中省略的links数组里,icon字段的值未正确关联到导入的Heroicons图标组件,导致const LinkIcon = link.icon得到的是undefined。
  • 在客户端组件中,React无法渲染undefined类型的元素,因此抛出该错误。添加'use client'后,组件从服务器组件转为客户端组件,原本服务器端的容错逻辑不再生效,错误直接暴露。

解决方法

  1. 确保links数组的icon字段正确引用已导入的图标组件,示例如下:
// 在'use client'组件的同一文件内定义links数组
const links = [
  { name: '主页', href: '/', icon: HomeIcon },
  { name: '团队', href: '/team', icon: UserGroupIcon },
  { name: '文档', href: '/docs', icon: DocumentDuplicateIcon },
];
  1. 保证links数组和图标组件的导入在同一个作用域内,避免因作用域问题导致icon字段值为undefined。

内容的提问来源于stack exchange,提问作者Kundera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 05:48:18