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

120
Vistas
React Native - Unable to set state with arrays

I'm trying to call external api during the first time when the component is rendered and get the list of all countries with async/await function. However I got empty state back after I called the setState function. I'm quite new to RN and been stuck in this problem for days now. Appreciated if anyone can help. Here's what I've been trying to do...

  1. Call countryapi and get the country name and iso2 (only country name and iso2 are available in the response)
  2. After that call I wanna get more details about that specific country, such as iso3 and country flag, so I passed the parameter iso2.
  3. After I destructured for country, iso3, and country flag after the second api call, I called setState each time I destructured for that specific country. However state is not getting updated.
import React, { useState, useEffect } from "react";
import {
  Platform,
  Text,
  View,
  TextInput,
  StyleSheet,
  FlatList,
} from "react-native";
import { FontAwesome } from "react-native-vector-icons";
import countryapi from "../src/countryapi";

const CountryScreen = () => {
  const [countryDetails, setCountryDetails] = useState([]);

  // Fetch country details { country name, iso3, iso2, country flag }
  const countrySearchAPI = async () => {
    try {
      const countriesData = await countryapi.get();
      const countries = countriesData.data;

      for (const country in countries) {
        const { iso2 } = countries[country];

        let countriesDetailsData = await countryapi.get(`/${iso2}`);
        let { name, iso3, emoji } = countriesDetailsData.data;

        setCountryDetails([
          ...countryDetails,
          {
            countryName: name,
            iso3,
            iso2,
            countryFlag: emoji,
          },
        ]);
        console.log(countryDetails);
      }
    } catch (e) {
      console.log(e);
    }

    console.log(countryDetails);
  };

  useEffect(() => {
    countrySearchAPI();
  }, []);

  return (
    <View>
      <View style={styles.searchSection}>
        <FontAwesome name="search" size={30} />
        <TextInput placeholder="Search for Country" style={styles.inputBox} />
      </View>
      {/* <FlatList
        data={countryDetails}
        renderItem={({ item }) => {
          return (
            <Text>
              {item.countryName}
              {item.countryFlag}
            </Text>
          );
        }}
        keyExtractor={(item) => item.id.toString()}
      /> */}
    </View>
  );
};
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