xcarpentier/react-native-country-picker-modal

get country selected by default

lasharela opened this issue ยท 3 comments

Is there any way to get the current country that is set by default? I would like to get the whole country object selected by default but won't like to getAllCountries and then filter.

Do we have better way to do so?

my solution was to fork this library and export this function also since it is already there and now it looks like this
Screenshot 2022-06-04 at 11 25 06
Screenshot 2022-06-04 at 11 25 44

import CountryPicker, {
  Country,
  CountryCode,
  FlagButton,
  getCountryInfo,
} from '@olegnjadocode/react-native-country-picker-modal'

const initLocalCountry: CountryCode = 'GB'

const [selectedCountry, setSelectedCountry] = useState<Country>()

  useEffect(() => {
    getCountryInfo({countryCode: initLocalCountry}).then((data) => {
      if (data) onSelectCountry(data)
    })
  }, [])

i guess i could try creating PR here to add this but ive seen that all recent PRs are closed due to inactivity so i dont think it will be accepted, you could try and install your own forked version or use mine https://www.npmjs.com/package/@olegnjadocode/react-native-country-picker-modal

but i also had to modify this function a little as it returns CountryInfo object, not full Country
Screenshot 2022-06-04 at 14 16 05

import CountryPicker, {
  getAllCountries,
  FlagType,
} from "react-native-country-picker-modal";

  useEffect(() => {
    async function getCountry() {
      const countries = await getAllCountries(FlagType.FLAT);
      const country = countries.find(
        (country) => country.cca2 === pickerCountryCode
      );

      setCountry(country);
    }
    getCountry();
  }, []);
import CountryPicker, {
  getAllCountries,
  FlagType,
} from "react-native-country-picker-modal";

  useEffect(() => {
    async function getCountry() {
      const countries = await getAllCountries(FlagType.FLAT);
      const country = countries.find(
        (country) => country.cca2 === pickerCountryCode
      );

      setCountry(country);
    }
    getCountry();
  }, []);

nice hack