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

329
Visualizações
How can I loop an API in react query and return all the responded data at once

I am trying to loop get API, the API takes a perameter and returns the is the parameter true or false.

I have used useEffect hook and looping the settingTypes and inside the loop I am calling the API and from the response, I am storing the values. I don't want to do like the below code because when the getSettingValues API is updated it's not re-fetching the API,

  const [activityTypes, setActivityTypes] = useState<any>([]);

  useEffect(() => {
    async function fetchData() {
      settingTypes.map(async (item: any) => {
        return await Api.user
          .getSettingValues(item.channelSettingTypeId)
          .then((response: any) => {
            setActivityTypes((oldArray: any) => [
              ...oldArray,
              {[item.channelSettingTypeId]: response.settingValue},
            ]);
          });
      });
    }
    fetchData();
  }, [activityFeedData.activityList, settingTypes]);

I am trying to do it using react query. Below the code is react query hook, useGetSettingValues receives a parameter and returns is the parameter true or false.

const useGetSettingValues = (params: any) =>
  useQuery([SETTING_VALUE, params], () => Api.user.getSettingValues(params), {
    initialData: {data: ''},
  });

My major issue is I want to loop the useGetSettingValues API and store all the response data in a state. as like the below code

await Api.user
          .getSettingValues(item.channelSettingTypeId)
          .then((response: any) => {
            setActivityTypes((oldArray: any) => [
              ...oldArray,
              {[item.channelSettingTypeId]: response.settingValue},
            ]);
          });

Any Idea how can I achieve it?

about 4 years ago · Juan Pablo Isaza
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