Closed fgagneten closed 4 years ago
I'm experiencing the exact same problem !
I need this feature too. Good job by the way.
//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 })}
/>
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.
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);
});
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.
I use the library like this:
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