React Phone Input 2 (currently documented for version 2.15.1) is a highly customizable React component designed for international phone number input with automatic formatting. It features a country dropdown with flag icons and dynamically formats the input based on the selected country, supporting various themes like Material UI and Bootstrap. The library is built on `libphonenumber-js` for accurate validation and adheres to international standards. While this entry focuses on version 2.15.1, it's important to note that a major version 3.x exists. This library distinguishes itself through extensive customization options for appearance and behavior, making it suitable for projects requiring a polished and interactive user experience with internationalization capabilities.
npm install react-phone-input-2Verified import paths — ran on the pinned version, not inferred.
Demonstrates a basic international phone number input with state management, country selection, auto-formatting, and a search feature in a functional React component.
Review the official changelog for `react-phone-input-2` v3 for specific migration steps. Test your application thoroughly after upgrading.
Add `import 'react-phone-input-2/lib/style.css';` (or your preferred theme) to your entry file or the component where `PhoneInput` is used.
Ensure the `value` prop is always a string. Convert other types to an empty string `''` if no number is present.
Monitor the project's GitHub repository for activity and consider contributing or sponsoring if relying heavily on the library. Factor this into long-term maintenance planning.
Utilize the `country` prop to explicitly set the default country, and potentially manipulate the initial `value` if it lacks a country code to ensure correct parsing.
Verify the exact path for the CSS file. Ensure `react-phone-input-2` is correctly installed. Common paths are `react-phone-input-2/lib/style.css` or `react-phone-input-2/lib/material.css`.
Always ensure the `value` prop is a string. If you're storing numbers, convert them to a string before passing, e.g., `value={String(myPhoneNumber)}`.Add `import 'react-phone-input-2/lib/style.css';` (or another theme like `material.css`) to your main application file or the component where `PhoneInput` is rendered.
Ensure `useState` is called directly within a functional component or a custom Hook. Verify that you don't have multiple versions of React installed, which can cause this error.