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

118
Views
React only adds to state when button is clicked again

bit of an odd issue, I have an action that updates the state, but this state is only updated when the button is clicked a second time.

   function addComponent(component: Component) {
        setSelectedComponents(oldArray => [...oldArray, component])

        console.log(selectedComponents)
    }

this is called when clicking on a button

    {components.map((component) => (
                                <div
                                    key={component.name}
                                    onClick={() => addComponent(component)}
                                    className="collapse w-96 border rounded-box border-base-300 collapse-plus my-2">
                                </div>
                            ))}

console.logging the result shows that, on the first click it's still an empty array and clicking the button again shows the object inside of the array.

any idea what's going on here? this is driving me crazy.

about 4 years ago · Juan Pablo Isaza
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!