Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda