Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Change active state of element on focus loose in react

I'm trying to create an animated Sidebar navigation. To do so I have 2 states for the link of the items. There is an active state and hover state. Active state is activated on click and hover is activated when the link is hovered, main problem I'm facing is that I'm using hash router to link to components on page. This makes it hard to implement a simple check like if(pathame === item.link.

import { FC, useEffect, useRef, useState } from 'react'
import { HashLink as Link } from 'react-router-hash-link'
import { SidebarItemProps } from '../../models/SidebarItem'

type SidebarLinkProps = {
  item: SidebarItemProps
}

const Submenu: FC<SidebarLinkProps> = ({ item }) => {
  const [active, setActive] = useState(false)
  const [hover, setHover] = useState(false)
  const link = useRef(null)
  useEffect(() => {
    if (document.activeElement === link.current) {
      setActive(true)
    } else {
      setActive(false)
    }
  }, [])

  return (
    <Link
      to={item.path}
      smooth
      ref={link}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      onClick={() => setActive(true)}
      className={
        'sidebar-link ' +
        (active || hover ? 'scale-up-center active' : 'scale-down-center')
      }
    >
      <span className="label">{item.title}</span>
    </Link>
  )
}

export default Submenu

this is my component currently I've tried using refs but since there are multiple items getting rendered here the document.activeElement and link.current is staying true when I click on a different element.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!