Skip to content

Commit

Permalink
Fix example active-class-anme: Add support for children of ActiveLink…
Browse files Browse the repository at this point in the history
… without a className. (#9271)

* Fix example active-class-anme: Add support for children of ActiveLink without a className.

* Update examples/active-class-name/components/ActiveLink.js

Co-Authored-By: Luis Alvarez D. <[email protected]>
  • Loading branch information
mgrubinger and Luis Alvarez D. committed Oct 31, 2019
1 parent 37ee8fc commit 81066a3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions examples/active-class-name/components/ActiveLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import React, { Children } from 'react'
const ActiveLink = ({ children, activeClassName, ...props }) => {
const { pathname } = useRouter()
const child = Children.only(children)
const childClassName = child.props.className || ''

const className =
pathname === props.href
? `${child.props.className} ${activeClassName}`
: child.props.className
? `${childClassName} ${activeClassName}`.trim()
: childClassName

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

ActiveLink.propTypes = {
Expand Down

0 comments on commit 81066a3

Please sign in to comment.