Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

326
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda