【发布时间】:2022-01-04 20:16:07
【问题描述】:
我希望我的导航栏为我所在的页面标题设置样式,例如,我使用 React 和 Tailwind CSS,当我在所选路径上时,只需将标题设为黄色。
我实现这一目标的逻辑是这样,但不起作用:
<div className={active ? "text-yellow-400" : undefined}
我的溃败代码:
const LinkItem = ({href, path, children, ...props}) => {
const active = path === href
return (
<NextLink href={href}>
<div className={active ? "text-yellow-400" : undefined}
{...props}
>
{children}
</div>
</NextLink>
)
}
导航条码:
const Navbar = props => {
const {path} = props
return (
<LinkItem href="/page1" path={path}>
Page 1
</LinkItem>
)
}
【问题讨论】:
-
尝试使用空字符串 (
"") 而不是undefined
标签: javascript css reactjs next.js tailwind-css