Skip to content

Instantly share code, notes, and snippets.

@srph
Last active June 20, 2022 07:34
Show Gist options
  • Save srph/9a4dacdf5a12c603e144a019d5ad06e4 to your computer and use it in GitHub Desktop.
Save srph/9a4dacdf5a12c603e144a019d5ad06e4 to your computer and use it in GitHub Desktop.
Next.js: Active link
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