mirror of
https://github.com/zhigang1992/react.git
synced 2026-03-26 22:42:51 +08:00
26 lines
605 B
TypeScript
26 lines
605 B
TypeScript
import React, { Children } from 'react'
|
|
import Link from 'next/link'
|
|
import { useRouter } from 'next/router'
|
|
|
|
export interface Props {
|
|
onAcitve?: Function
|
|
href: string
|
|
}
|
|
|
|
const ActiveLink: React.FC<React.PropsWithChildren<Props>> = React.memo(
|
|
({ children, href }) => {
|
|
const { pathname } = useRouter()
|
|
const isActive = pathname === href
|
|
const child = Children.only(children)
|
|
|
|
return (
|
|
<Link href={href}>
|
|
{React.cloneElement(child as React.ReactElement, {
|
|
className: isActive ? 'active' : null,
|
|
})}
|
|
</Link>
|
|
)
|
|
})
|
|
|
|
export default ActiveLink
|