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

166
Views
I want to access my state object after the setState Initializes which state system should i use

My Color Game

I want to get the value of set state but componentDidMount just gives me the previous value of state without updating it

Can anyone tell me which state system to use to resolve this problem Thanxs

    state = {
      
        color: '',
        main_color: '',


    }

    componentDidMount() {
    

        let box_count = 3;

        let byee = document.querySelectorAll(".boxes");

        let main_color = this.state.main_color;
        for (let i = 0; i < box_count; i++) {
            byee[i].addEventListener("click", function () {

                if (byee[i].style.backgroundColor === main_color) {

                    win();

                } else {

                    loose();
                     
                    console.log(byee[i].style.backgroundColor);
                    console.log(main_color);
                }
            })
        }
        function win() {
            console.log("winner");
        }
        function loose() {
            console.log("loose");
        }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Change:

let main_color = this.State.main_color;

To:

let main_color = this.state.main_color;

Notice to State which should use as state without capital letter.

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!