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

71
Views
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 answers
Answer question

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 Report

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 Report
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!