Skip to main content

Hooks

usePhoneInput(options)

A headless hook exposing the same state, formatting and smart-paste pipeline that powers <PhoneInput />. Use it when you need a fully custom UI.

Options — UsePhoneInputOptions

OptionTypeDescription
defaultCountryICountryCca2Initial country (ISO 3166-1 alpha-2)
defaultPhoneNumberstringInitial E.164 phone number
defaultValuestringDeprecated alias of defaultPhoneNumber
valuestringControlled phone value
countryICountry | nullControlled country
customMaskstringCustom mask pattern (e.g. (###) ###-####)
onChangePhoneNumber(phoneNumber: string) => voidFires when the formatted national value changes
onChangeCountry(country: ICountry) => voidFires when the country changes
onPhoneNumberTypeChange(type: PhoneNumberType | null) => voidFires when the detected line type changes
onValidationChange(isValid: boolean, type: PhoneNumberType | null, country: ICountry) => voidFires on validity transitions

Returns — UsePhoneInputResult

Property / MethodTypeDescription
nationalPhoneNumberstringNational number without calling code (digits only)
nationalPhoneNumberFormattedstringNational number with formatting applied
internationalPhoneNumberstringCalling code + national number (digits only)
internationalPhoneNumberFormattedstringCalling code + national number with formatting
internationalPhoneNumberLengthnumberTotal digit count
countryICountryCurrently selected country object
isValidPhoneNumberbooleanWhether the current number is valid for the country
phoneNumberTypePhoneNumberType | nullDetected line type, or null if incomplete/invalid
setCountry(country: ICountry) => voidSet the country imperatively
setPhoneNumber(value: string, options?: { emitChange?: boolean }) => voidSet the phone number imperatively. Routes through the smart-paste pipeline (detects E.164, switches country, formats). Pass { emitChange: false } to suppress the onChangePhoneNumber callback.
onChangePhoneNumber(text: string) => voidHandler ready to wire into a TextInput's onChangeText

Example

import {TextInput, View} from 'react-native';
import {usePhoneInput} from 'rn-international-phone-number';

export default function CustomPhoneInput() {
const {
country,
nationalPhoneNumberFormatted,
isValidPhoneNumber,
onChangePhoneNumber,
} = usePhoneInput({defaultCountry: 'US'});

return (
<View>
<TextInput
value={nationalPhoneNumberFormatted}
onChangeText={onChangePhoneNumber}
placeholder={`${country?.callingCode} phone`}
/>
</View>
);
}