Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

activeClassName in next.js

import Link from "next/link";
import { useRouter } from "next/router";


export const MyNav = () => {

  const router = useRouter();

  return (
    <ul>
      <li className={router.pathname == "/" ? "active" : ""}>
        <Link href="/">home</Link>
      </li>
      <li className={router.pathname == "/about" ? "active" : ""}>
        <Link href="/about">about</Link>
      </li>
    </ul>
  );
};
Comment

activeClassName in next.js


import { useRouter } from 'next/router'
import PropTypes from 'prop-types'
import Link from 'next/link'
import React, { Children } from 'react'

const ActiveLink = ({ children, activeClassName, ...props }) => {
  const { asPath } = useRouter()
  const child = Children.only(children)
  const childClassName = child.props.className || ''

  // pages/index.js will be matched via props.href
  // pages/about.js will be matched via props.href
  // pages/[slug].js will be matched via props.as
  const className =
    asPath === props.href || asPath === props.as
      ? `${childClassName} ${activeClassName}`.trim()
      : childClassName

  return (
    <Link {...props}>
      {React.cloneElement(child, {
        className: className || null,
      })}
    </Link>
  )
}

ActiveLink.propTypes = {
  activeClassName: PropTypes.string.isRequired,
}

export default ActiveLink

Comment

PREVIOUS NEXT
Code Example
Javascript :: fb login npm 
Javascript :: access laravel eloquent relation in js 
Javascript :: redirect route after registration on mysql by axios post method 
Javascript :: angular declare variable in a file 
Javascript :: javascript check if key exists in object 
Javascript :: static folder express 
Javascript :: module build failed (from ./node_modules/css-loader/dist/cjs.js): 
Javascript :: javascript remove period from end of string 
Javascript :: Uncaught ReferenceError: function is not defined at HTMLUnknownElement.onclick 
Javascript :: change mouse highlight color js 
Javascript :: get attribute href 
Javascript :: How to create $(document).ready() for vanilla JavaScript 
Javascript :: discord.js v13 
Javascript :: javascript array value dom 
Javascript :: javascript redirect browser 
Javascript :: vue test utils emitted 
Javascript :: discord.js calculator command 
Javascript :: javascript parentnode 
Javascript :: sort multidimensional array javascript 
Javascript :: javascript set readonly property 
Javascript :: dummy json data 
Javascript :: select random from an array 
Javascript :: how to copy text in the clipboard in js 
Javascript :: js date after 1 year 
Javascript :: discord.js bot 
Javascript :: how to extract year from utc in javascript 
Javascript :: local storage size check 
Javascript :: for each element in object 
Javascript :: javascript display max amount of characters 
Javascript :: Javascript file in html angeben 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =