Files
f260328900 BREAKING CHANGE: Enable newNextLinkBehavior (#41459)
- Enable newNextLinkBehavior. See #36436 
- Run next/link codemod on test suite

Note that from when this lands on apps trying canary will need to run
the new-link codemod in order to upgrade.
Ideally we have to detect `<a>` while rendering the new link and warn
for it.

Co-authored-by: Steven <[email protected]>
2022-10-17 21:20:28 -04:00

85 lines
2.0 KiB
JavaScript

import * as React from 'react'
import Router, { withRouter } from 'next/router'
import Link from 'next/link'
const pages = {
'/a': 'Foo',
'/b': 'Bar',
}
class HeaderNav extends React.Component {
constructor({ router }) {
super()
this.state = {
activeURL: router.asPath,
activeURLTopLevelRouterDeprecatedBehavior: router.asPath,
activeURLTopLevelRouter: router.asPath,
}
}
componentDidMount() {
Router.onRouteChangeComplete =
this.handleRouteChangeTopLevelRouterDeprecatedBehavior
Router.events.on(
'routeChangeComplete',
this.handleRouteChangeTopLevelRouter
)
this.props.router.events.on('routeChangeComplete', this.handleRouteChange)
}
componentWillUnmount() {
Router.onRouteChangeComplete = null
Router.events.off(
'routeChangeComplete',
this.handleRouteChangeTopLevelRouter
)
this.props.router.events.off('routeChangeComplete', this.handleRouteChange)
}
handleRouteChange = (url) => {
this.setState({
activeURL: url,
})
}
handleRouteChangeTopLevelRouter = (url) => {
this.setState({
activeURLTopLevelRouter: url,
})
}
handleRouteChangeTopLevelRouterDeprecatedBehavior = (url) => {
this.setState({
activeURLTopLevelRouterDeprecatedBehavior: url,
})
}
render() {
return (
<nav>
{Object.keys(pages).map((url) => (
<Link
href={url}
key={url}
prefetch
className={`${this.state.activeURL === url ? 'active' : ''} ${
this.state.activeURLTopLevelRouter === url
? 'active-top-level-router'
: ''
} ${
this.state.activeURLTopLevelRouterDeprecatedBehavior === url
? 'active-top-level-router-deprecated-behavior'
: ''
}`}
>
{pages[url]}
</Link>
))}
</nav>
)
}
}
export default withRouter(HeaderNav)