Last active
June 20, 2022 07:34
-
-
Save srph/9a4dacdf5a12c603e144a019d5ad06e4 to your computer and use it in GitHub Desktop.
Next.js: Active link
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { pathname } = useRouter() | |
const active: Record<string, boolean> = useMemo(() => { | |
return [ | |
{ key: 'home', url: '/' }, | |
{ key: 'about', url: '/about' }, | |
{ key: 'contact', url: '/contact' } | |
].reduce((object, link) => { | |
object[link.key] = pathname === link.url | |
return object | |
}, {}) | |
}, [pathname]) | |
<a className={cx({ 'text-blue': active.home, 'text-gray': !active.home })} /> | |
<a className={cx({ 'text-blue': active.about, 'text-gray': !active.about })} /> | |
<a className={cx({ 'text-blue': active.contact, 'text-gray': !active.contact })} /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment