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

100
Views
setState is not working on 2nd time calling it inside componentDIdMount

im a beginner, im trying to make a memory game this component fetches data for an api then trims it down with only that has image link

then on level one it should display 3 random image from fetch data it always displayedChars: [undefined, undefined, undefined]

    constructor(props) {
       super()
       this.state = {
             level: 1,
             numImg: 1*3,
             displayedChars: [],
             chars: []
       }

    }


      async componentDidMount() {
        await this.loadData().then(data => {
          this.setState({
            chars: this.trimData(data)
          });
        });
    
        await this.displayChars().then(data => {
          console.log(data)
          this.setState({
            displayedChars: data
          });
        });
        
        console.log(this.state);
      }

     async loadData() {
       try {
         const res = await fetch(`http://hp-api.herokuapp.com/api/characters`)
         const characters = await res.json();
         return characters
       } catch(err) {
         console.error(err)
       }
     }

     trimData(characters) {
       const listChars = []
         characters.map(char => {
           if(char.image !== "") {
             listChars.push(char)
           }
         })
         return listChars
     }

     displayChars() {
         return (new Promise((resolve) => {
           const list = []
           for(let x=1; x<= this.state.numImg; x++) {
             let randomNum = Math.floor(Math.random() * 24);
             list.push(this.state.chars[randomNum]);
           }
           console.log(list)
           resolve(list)
         }))

    }

in the this.displayChars() console.log(data) works fine but

          this.setState({
            displayedChars: data
          });

then console.log(this.state) OUTPUT: [undefined, undefined, undefined]

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

0

setState is async, so you cannot see the updated states immediately, that's why your console.log is [undefined, undefined, undefined]. You can assign variables to handle responses separately instead of using this.state.

The second concern is you shouldn't mix then and async/await. I'd prefer using async/await alone in your case.

async componentDidMount() {
        const chars = await this.loadData();
        const displayedChars = await this.displayChars();
        
        this.setState({
            displayedChars: displayedChars
            chars: this.trimData(chars)
        });
        
        //states are not updated right away
        //console.log(this.state);

        //access via variable
        console.log({ chars, displayedChars })
      }
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!