xcarpentier / react-native-country-picker-modal

🇦🇶 Country picker provides a modal allowing a user to select a country from a list. It display a flag next to each country name.
https://reactnative.gallery/xcarpentier/country-picker
MIT License
1.07k stars 796 forks source link

get country selected by default #471

Open lasharela opened 2 years ago

lasharela commented 2 years ago

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?

OlehDeneka-docode commented 2 years ago

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
quyentho commented 11 months ago
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();
  }, []);
israromar commented 9 months ago
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