import {allCountries} from 'country-telephone-data';
import * as Localization from 'expo-localization';
import React, {useEffect, useState} from 'react';
import {Text} from 'react-native';
export const CountryDialCode = () => {
const [dialCode, setDialCode] = useState<string>();
// This sets a state variable for dial code with an async call that looks up the localisation region and then uses that to look up the country dial code.
useEffect(() => {
const getCountryTelephoneCode = async () => {
const {region} = await Localization.getLocalizationAsync();
if (!region) return;
const country = allCountries.find((country) => country.iso2 === region.toLowerCase());
console.log(country);
const _dialCode = country?.dialCode;
setDialCode(_dialCode);
};
getCountryTelephoneCode();
}, []);
return <Text>{dialCode}</Text>;
};
2条答案
按热度按时间iqjalb3h1#
您可以使用catamphetamine/libphonenumber-js。例如:
42fyovps2#
您可以使用npm库,如
country-telephone-data
下面是一个如何使用
expo-localization
执行此操作的示例...但原理与react-native-localize
非常相似