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

202
Views
I can't set the state in my handler function

I'm trying to set a const of my state but it seems that don't work when a few days ago it worked.

I call the setter in a function like this:

const [activeMarker, setActiveMarker] = useState(null);

const handleActiveMarker = (marker) => {
  if (marker === activeMarker) {
     return;
  }
  setActiveMarker(marker);
  console.log(marker, '--', activeMarker)
};

That is called in

{markers.map(({ id, latitud, longitud}) => (
   <Marker
      key={id}
      position={{lat: latitud, lng: longitud}}
      onClick={() => handleActiveMarker(id) }
   />
))}

The attribute markers is an array of markers and it's printed well in my map, but when I click, in console.log of my handler I get the id of marker and null in activeMarker where it has to appear the same number.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Remember useState is asynchronous so it will not update straight away. To get the console.log value to log your value you should add a useEffect.

    useEffect(() => {
        console.log(activeMarker)
    },[activeMarker])
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!