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

226
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
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