我想要完成的是在更改路由器时与github上的这个 React 应用程序完全相同的打开和关闭菜单动画,但是使用 Next.js(它几乎是相同的代码)
这是我在 github 上的应用程序仓库,这里是应用程序。
我正在尝试关注Next.js 文档,但似乎状态menuOpened
未更新。
import Link from 'next/link'
import React, { useEffect, useState } from 'react'
import { withRouter, useRouter } from 'next/router'
import { UpArrowCircle } from '@/components/SvgFiles'
import { openMenu, closeMenu } from '@/components/MenuAnimations'
const Header = ({ dimensions }) => {
const [menuState, setMenuState] = useState({ menuOpened: false })
const router = useRouter()
useEffect(() => {
const handleRouteChange = (url) => {
// console.log(`App is changing to ${url}`)
setMenuState({ menuOpened: false })
}
if (menuState.menuOpened === true) {
openMenu(dimensions.width)
} else if (menuState.menuOpened === false) {
closeMenu()
}
router.events.on('routeChangeStart', handleRouteChange)
return () => {
router.events.off('routeChangeStart', handleRouteChange)
}
})
return (
<header className="header">
<div className="container">
<div className="row v-center space-between">
<div className="logo">
<Link href="/">
<a>AGENCY</a>
</Link>
</div>
<div className="nav-toggle">
<div
onClick={() => setMenuState({ menuOpened: true })}
className="hamburger-menu"
>
<span></span>
<span></span>
</div>
<div
className="hamburger-menu-close"
onClick={() => setMenuState({ menuOpened: false })}
>
<UpArrowCircle />
</div>
</div>
</div>
</div>
</header>
)
}
export default withRouter(Header)
欢迎任何帮助!干杯!