DEV Community

Discussion on: Post an Elegant Code Snippet You Love.

Collapse
 
franklivania profile image
Chibuzo Franklin Odigbo

This is my best code snippet yet. It does exactly what you see it does

export default function PageToggle({className, title, image, link}: PageTypes) {
    const location = useLocation()
    const isActive:boolean = location.pathname === link 

    function handleClick() {
        window.location.href = link
    }
    return(
        <PageButton
            type='button'
            className={`${className} ${isActive ? 'active' : ''}`}
            onClick={handleClick}
        >
            {image && <img src={image} alt="" />}
            {title}
        </PageButton>
    )
}
Enter fullscreen mode Exit fullscreen mode