【发布时间】:2022-12-06 18:52:21
【问题描述】:
在我的nextjs-app 中,我有一个Navbar-组件,我在其中使用useEffect-hook 来检查<body>-tag 是否包含一个类:
导航栏.tsx:
const Navbar = () => {
const router = useRouter()
const [fullWidthBackground, setFullWidthBackground] = useState(false)
useEffect(() => {
const isFullWidth = document.body.classList.contains('full-width-hero')
if (isFullWidth) {
setFullWidthBackground(true)
} else {
setFullWidthBackground(false)
}
}, [fullWidthBackground])
}
到目前为止,这似乎有效,但是当我在具有 full-width-hero-class 的路由/页面上并且它们导航到 /-route(页面 -> index.tsx)时,它没有full-width-hero-类,isFullWidth 仍然返回 true - 那么这里发生了什么?
有人可以帮我吗
【问题讨论】:
标签: javascript reactjs next.js