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

130
Views
empty useState on request React

I am using a button to perform a fetch request, when I do it and try to set the response in useState it always stays empty but if I click the button again now it sends me the information correctly.

This is my code:


  const fetchUsers = async (page, limit = perPage, search = searchBox, type) => {

    if (type === 'all') {
      limit = 100000
    } else {
      setLoading(true);
    }

    const dataOnLs = localStorage.getItem('cmtjs')

    const config = {
      headers: {
          "Content-Type": "application/json",
          apiKey: dataOnLs
      } ,
      params: {
          limit,
          page, 
          search
        }            
  }
    const response = await axiosClient(`/products`, config)
    console.log(response.data.products.docs); // DATA OK
    if (type === 'all') {
      setDataToXLS(response.data.products.docs); // **EMPTY**
    } else {
      setData(response.data.products.docs); // DATA OK
      setTotalRows(response.data.products.totalDocs);
      setLoading(false);
    }
  };

I don't understand why it doesn't set my useState (setDataToXLS) with the data on the first click and I have to click twice to get the data correctly

about 4 years ago · Santiago Gelvez
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!