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.08k stars 802 forks source link

Get the value of Country Picker #305

Closed fgagneten closed 4 years ago

fgagneten commented 4 years ago

I use the library like this:

 <CountryPicker
        withCallingCode={true}
        withEmoji={true}
        countryCode="AR"
        visible={this.state.pickerVisible}
        onSelect={country =>
          this.onChangeCountry(country.cca2, country.callingCode)
        }
        onClose={() => this.setState({ pickerVisible: false })}
      />

This code works great. How ever, as you can see, I set the countryCode "AR" by default (so the Argentinian flag show ups when the component was created). However, I have no ways to get the calling code of a already defined country code. I can only get the calling code if onSelect is executed. But the first time will not execute.

So, How can I get from the country code, get the calling code the first time?

Thanks

bartolomej commented 4 years ago

I'm experiencing the exact same problem !

tisch7 commented 4 years ago

I need this feature too. Good job by the way.

shiny-mountain commented 4 years ago

//default state

state = {
    country: {
         cca2: 'AR',
    }
}

// render

 <CountryPicker
        withCallingCode={true}
        withEmoji={true}
        countryCode={this.state.country.cca2}
        visible={this.state.pickerVisible}
        onSelect={country =>
          this.setState({ country })
        }
        onClose={() => this.setState({ pickerVisible: false })}
      />
hinciler commented 4 years ago

I found this solution to get callingcode

import * as RNLocalize from 'react-native-localize';
import CountryPicker, { getCallingCode } from 'react-native-country-picker-modal';

const userCountryCode = RNLocalize.getCountry();
getCallingCode(userCountryCode).then(cCode => {
      this.setState({ callingCode: cCode });
});

I hope this will help your issue.

agungsb commented 4 years ago

In case you want to get the value of the country, you can use getAllCountries;

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

getAllCountries().then((countries) => {
      const country = countries.find((c) => (c.cca2 === countryCode));
      console.log('country', country);
});
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.