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

152
Views
React setState hook's previous param object gives inconsistent values

I'm using a useEffect hook to trigger a setState update. I'm getting some strange and inconsistent behaviour from the previous param:

    useEffect(() => {
        setCurrentPicturesObject((existing) => {
            const clone = {...existing}
            console.log({
                existing,
                existingdotNocolor: existing.nocolor,
                selectedColorState,
                selectedColorArray: existing["nocolor"],
                clone
            });
            return existing
        });
    }, [selectedColorState]);

So you'd expect that the clone of the object would return an object with the same keys and values, right? Not here:

screenshot of console log

Somehow existing goes from being an object with a nocolor prop with an array of two strings, to switching to an array with one string. Similarly when I try to access the nocolor prop it only returns an array with one string.

I can't understand. existing changes as soon as I try to access it with anything other than simply console logging it directly?

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

0

Not entirely sure on the details, but essentially it seems that the console is logging a live view of the object, which is a static string. More info in this answer.

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!