React Phone Number Input
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.
Common errors
-
Module not found: Can't resolve 'react-phone-number-input/style.css'
cause The necessary CSS stylesheet for the component's visual styling has not been imported into the application.fixAdd `import 'react-phone-number-input/style.css';` to a central CSS import file or directly into the component where `PhoneInput` is being used. -
TypeError: Cannot read properties of undefined (reading 'length') or similar errors related to `value` prop.
cause The `value` prop passed to `PhoneInput` is either not provided, is `null`, or is of an incorrect type (e.g., an object instead of a string or `undefined`), leading to unexpected behavior in `libphonenumber-js`.fixEnsure 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. -
React Hook "useState" cannot be called inside a class component.
cause You are attempting to use React Hooks (such as `useState`) within a class-based React component, which is not supported by React.fixRefactor your component to be a functional component to utilize Hooks, or manage state using the class component's `this.state` and `this.setState` mechanisms.
Warnings
- breaking Major version 3.x introduced significant breaking changes compared to 2.x, including API alterations, styling adjustments, and internal component structure. Direct upgrades without consulting the changelog will likely result in errors.
- gotcha The `value` prop and the `onChange` callback's argument strictly use the E.164 format string (e.g., '+12133734253') for valid numbers. For an empty or cleared input, `onChange` will return `undefined`, not `null` or an empty string. Misinterpreting `undefined` can lead to incorrect state management or UI behavior.
- gotcha The default `PhoneInput` export includes the country select dropdown. If you only require a basic phone number input field without this feature (e.g., for custom UI or smaller bundle size), you must use the specific import `from 'react-phone-number-input/input'`. Using the default export unnecessarily can increase bundle size.
- gotcha The underlying `libphonenumber-js` library can significantly increase your application's bundle size if all locale data for every country is loaded. This can impact initial load performance, especially for web applications.
Install
-
npm install react-phone-number-input -
yarn add react-phone-number-input -
pnpm add react-phone-number-input
Imports
- PhoneInput (with country select)
import { PhoneInput } from 'react-phone-number-input'import PhoneInput from 'react-phone-number-input'
- PhoneInput (basic input)
import { PhoneInput } } from 'react-phone-number-input/input'import PhoneInput from 'react-phone-number-input/input'
- PhoneInputWithCountrySelect (memoized)
import PhoneInputWithCountrySelect from 'react-phone-number-input'
import { PhoneInputWithCountrySelect } from 'react-phone-number-input' - CSS styles
import 'react-phone-number-input/style.css'
Quickstart
import React, { useState } from 'react';
import 'react-phone-number-input/style.css';
import PhoneInput from 'react-phone-number-input';
function App() {
const [value, setValue] = useState();
// `value` will be the parsed phone number in E.164 format (e.g., "+12133734253").
// For an empty input, `value` will be `undefined`.
return (
<div style={{ maxWidth: '400px', margin: '50px auto', fontFamily: 'sans-serif' }}>
<h1>International Phone Input</h1>
<p>Enter your phone number below:</p>
<PhoneInput
placeholder="Enter phone number"
value={value}
onChange={setValue}
defaultCountry="US"
international
countryCallingCodeEditable={false}
/>
{value && <p>Parsed E.164 Value: <code>{value}</code></p>}
{!value && <p>Input is currently empty or invalid.</p>}
</div>
);
}
export default App;