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

288
Views
why useState return empty array in react.js?

I am trying to receive an array from useState when I click the first time on submit ,the usestate return an empty array and this make a probleme ?

const [order,setOrder] = useState([]);

const submit = (e) => {

 var NU =document.querySelector('#orderDesignation').value;
  var d =document.querySelector('#orderPrix').value;
   var p = document.querySelector('#origin').value;

     var q =document.querySelector('#orderQuntite').value;
    var t =document.querySelector('#orderPrixHT').value;


const  s = { orderDesignation: "d" ,orderPrix: "p" ,orderQuntite: "q" ,orderPrixHT:"t" }

console.log("s -->",s);

//s --> Object { orderDesignation: "dss", orderPrix: "1", orderQuntite: "3", orderPrixHT: 2 }

setOrder(order => [...order,s] );
 
console.log("order =",order);   //    order = Array[]   empty !!!! ?

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

0

Because setOrder command is not a immediate command ,React may delay it and because your console.log is in next line in same function, it will returns the default value of order that is empty array.

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!