You need to enable JavaScript to run this app. Next.js使用Link组件时className收到非布尔值false警告如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Next.js使用Link组件时className收到非布尔值false警告如何解决

报错原因与解决方案

问题根因

你代码中<a>标签的className属性使用了条件 && 类名字符串的简写写法,当router.pathname == classPath判断结果为否时,整个表达式会返回布尔值false。React 不允许将布尔值直接传递给 className 这类 DOM 原生属性,因此触发对应报错。

修复代码

将&&条件写法替换为三目运算符,条件不成立时返回undefined即可,修改后的完整代码如下:

import React from 'react';
import Link from 'next/link';
import { useRouter } from 'next/router';

const SellerSidebarData = ({ classPath, path, icon, title }) => {
  const router = useRouter();

  return (
    <li>
      <Link href={path}>
        <a
          className={
            router.pathname === classPath 
              ? 'wave-effect waves-effect waves-button active' 
              : undefined
          }
        >
          <i aria-hidden className={`width18 textCenter ${icon}`}></i>
          {title}
        </a>
      </Link>
    </li>
  );
};

export default SellerSidebarData;

补充说明

  • 这里额外将宽松相等==替换为严格相等===,避免隐式类型转换带来的意外匹配问题
  • 若你需要保留className属性仅值为空,可将undefined替换为空字符串''

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 01:33:00