react-phone-number-input is a comprehensive React component designed for entering and formatting international telephone numbers. It leverages the robust `libphonenumber-js` library internally for accurate parsing, validation, and formatting, ensuring numbers are consistently output in the E.164 format (e.g., "+12133734253"). The package is currently at version 3.4.16 and demonstrates an active release cadence, indicating ongoing development and maintenance. Its key differentiators include offering both a full-featured input with an international country select dropdown and a basic input-only variant, alongside native UI integration for country selection on mobile devices. The component effectively handles diverse country codes, various number formats, and provides clear mechanisms for controlling default country selections and managing input changes via its props.
npm install react-phone-number-inputVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a functional React component using the default `PhoneInput` (with country select). It shows how to manage the input's value using React state and displays the resulting E.164 formatted number, including necessary CSS import and default country setting.
Review the official `CHANGELOG.md` thoroughly for specific migration steps and updated API usage when upgrading from 2.x to 3.x. Adjust component props and imports as needed.
Ensure your state management and validation logic explicitly checks for `undefined` when expecting an empty or cleared input value. Always initialize state for `value` as `undefined` or an E.164 string.
For a basic input without country selection, always import `PhoneInput from 'react-phone-number-input/input'`. For the full component, use `import PhoneInput from 'react-phone-number-input'` or `import { PhoneInputWithCountrySelect } from 'react-phone-number-input'`.Implement selective data loading for `libphonenumber-js`. Consider importing only specific country data or using smaller data subsets (e.g., `import 'libphonenumber-js/min'` for minimal data, or `import 'libphonenumber-js/max'` for more common data without all edge cases), or dynamic imports for less frequently used data.
Add `import 'react-phone-number-input/style.css';` to a central CSS import file or directly into the component where `PhoneInput` is being used.
Ensure the `value` prop is always a string in E.164 format (e.g., `"+1234567890"`), or `undefined` to represent an empty input. Always initialize the state variable managing `value` accordingly.
Refactor your component to be a functional component to utilize Hooks, or manage state using the class component's `this.state` and `this.setState` mechanisms.