Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda