Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
Style div in React's jsx as per the id/ class name, which will check condition

So, here is what exactly I am trying to do...
I am sending the class name and id using props. and by checking the condition the particular style will be implemented to it.
(this code is used to implement Navigation bar)
so that my active class (Menu Tab) will be highlighted only.

but, when I used this it will highlight all navigation tabs at the same time.

export default function NavElements(props) {

    const history = useLocation();
    // console.log(history);

    const [isActive, setIsActive] = useState(props.isActive);

    useEffect(() => {
        setIsActive(props.isActive);
    },[props.isActive]);

    function handleClick() {
        props.onClick(props.index);
    }
    return (
        <Link to={`${props.to}`}>
            <div id={`${props.rectangle}`} onClick={handleClick} className={ isActive ? 'backgroundSelected' : ''}>
                <div className="buttons-style" id={`${props.myClassName}`}>
                    <div className="text-align">
                        <div className="text-link">{props.Title}</div>
                    </div>
                </div>
            </div>
        </Link>
    )
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can also use useLocation as you defined also

export default function NavElements(props) {

    let location = useLocation();
    // console.log(history);


    return (
        <Link to={`${props.to}` className={`${location.pathname === props.to }? "active":""`}}>
            <div id={`${props.rectangle}`}>
                <div className="buttons-style" id={`${props.myClassName}`}>
                    <div className="text-align">
                        <div className="text-link">{props.Title}</div>
                    </div>
                </div>
            </div>
        </Link>
    )
}

The Logic is whenver the pathname equals to the endpoint directed by any <Link> , it gets active.

You can define your own class inplace of active class of if you want

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm a little bit confused about what you are trying to do. But if you want highlight the active menu, why can't you use NavLink instead of Link?. The NavLink has a property isActive which return true if the menu is active.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda