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

205
Views
React - console logs empty an array and then logs a populated array after a second button click

I am running into a slight problem with using React and its hooks. I am trying to print out an array from an API, but it first prints as an empty array to the console, and then only when I click the button again does it prints the array.

Here is the function I'm using to make the array from API data:

 const getChampion = () => {
        for(let i = 0; i < getData.length; i++){
            let individualChamp = champPeep.current.value;
            if(getData[i].Name === individualChamp){
                // console.log(getData[i]);

                setShowChampion(individualChamp);
                setChampionTitle(getData[i].Title);

                let hitPoints = getData[i].Hp
                let attack = getData[i].Attack;
                let defense = getData[i].Defense;
                let attackRange = getData[i].AttackRange;
                let armor = getData[i].Armor;

                setRadarData([hitPoints, attack, defense, attackRange, armor]);
                console.log(radarData) //returns empty array don't know why
            }

        } //! Have to click search twice to update array to new array

    } //Get Champion name and check to see if it is found in the API

Here is the button the input field that I assigned to this function:

return(
 <div>
                    <div className='search'>
                        <input ref={champPeep} type="search" id='champion-search' placeholder='e.g Annie' />
                    </div>
                    <button onClick={getChampion} className='btn-prim'>Search</button>
                </div>
)

And this is what is being logged to the console when I click on button btn-prim:

[]

And when I click the btn-prim button again this is then logged (which is correct):

(5) [524, 2, 3, 625, 19]

Is there something I'm doing wrong?

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

0

setState is asynchronous in react, so when you try to log radarData immediately after setRadarData it displays previous data stored in radarData. You can use useEffect hook to log current radarData state

useEffect(() => {
   console.log(radarData)
}, [radarData])

why React setStates are async : Why is setState in reactjs Async instead of Sync?

about 4 years ago · Juan Pablo Isaza Report

0

I suggest that instead of you using

console.log(radarData) //returns empty array don't know why

try to add the useEffect hook to log the value of radarData whenever it changed.

Use something like:

useEffect(() => {console.log(radarData)}, [radarData])
about 4 years ago · Juan Pablo Isaza Report

0

State updates will reflect in their next rerender and not immediately. This has already been solved.

Basically your

setRadarData([hitPoints, attack, defense, attackRange, armor]);
console.log(radarData) //returns empty array because its still using the default state {}.

Refer to The useState set method is not reflecting a change immediately.

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!