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

68
Visualizações
toogle component on click with react hooks

I'm toggling a component while clicking on div . It toogle down when clicked but does not close when clicked again what am i doing wrong?

export default function Shiva() {
    const [loan, setLoan] = useState(false);
    const handleClick = () => {
        setLoan(true);      
    }
return (
<div className="">
    <div className="">
        <div className="loan-type-select" onClick={handleClick}>
            <div className="">
                <img src="" />
            </div>
            <p>SBA 7A Loan</p>
            {loan ? <Data /> : '' }
        </div>
    </div>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Look closely at your click handler:

const handleClick = () => {
    setLoan(true);      
}

Regardless of the current state of loan, it will always set the new state of loan to true.

If you want to close it when it's open, toggle the current state instead of passing true.

const handleClick = () => {
    setLoan(!loan);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

I would like to add that instead of using:

setLoan(!loan)

It is better to use:

setLoan((preValue)=>{!preValue})

This way you make sure you won't face any issues (you are changing the previous value instead of changing the actual value).

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