React International Phone Input
React International Phone is a lightweight and easy-to-integrate React component for handling international phone number inputs. It's currently at stable version 4.8.0, with frequent minor and patch releases to update country data, fix bugs, and add small features. Key differentiators include its small bundle size with no third-party runtime dependencies, automatic country guessing based on input, comprehensive validation utilities, and flexible customization options. It also provides a headless hook (`usePhoneInput`) for integrating with any UI library, allowing developers to maintain consistent styling while leveraging the library's core functionality for phone number formatting and country detection. It ships with TypeScript types for enhanced developer experience.
Common errors
-
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
cause Attempting to use `PhoneInput` with an incorrect import statement, typically `import PhoneInput from 'react-international-phone';` instead of a named import.fixChange your import statement to `import { PhoneInput } from 'react-international-phone';` -
TypeError: Cannot read properties of undefined (reading 'split') or similar errors related to country data.
cause This error often occurs if the country data or formatting utilities are not correctly initialized or accessed, possibly due to a missing dependency or an issue in how the library's internal data is consumed, especially in server-side rendering (SSR) environments if not configured correctly.fixEnsure `react-international-phone/style.css` is imported and that no custom build steps are interfering with module resolution. If in SSR, check the library's SSR compatibility documentation. -
Type 'string' is not assignable to type '...'. Property 'onChange' is missing in type '...' but required in type '...'
cause Incorrectly typing component props in TypeScript, or providing a value that doesn't match the expected type, specifically for the `PhoneInput`'s `value` or `onChange` props.fixEnsure `value` is a string (the full international number) and `onChange` accepts a string parameter, matching the `PhoneInput` component's type definitions. For example: `onChange={(phone: string) => setPhone(phone)}`.
Warnings
- breaking Migration from v3 to v4 introduced breaking changes. Key areas affected were likely API surface area changes for props or return values from hooks, and potentially internal structure modifications.
- breaking Upgrading from v2 to v3 involved breaking changes that required adjustments to component usage or styling. This often includes prop renames, removal of deprecated features, or changes in default behavior.
- breaking The initial major version update from v1 to v2 likely included significant API changes. This could involve complete rewrites of components or hooks, and major refactors of internal logic.
- gotcha The default styles for the `PhoneInput` component must be explicitly imported. Forgetting this import will result in an unstyled component.
- gotcha When using the `usePhoneInput` headless hook, you are responsible for rendering all UI elements (input, country selector, flags) and connecting them to the hook's return values.
Install
-
npm install react-international-phone -
yarn add react-international-phone -
pnpm add react-international-phone
Imports
- PhoneInput
const { PhoneInput } = require('react-international-phone');import { PhoneInput } from 'react-international-phone'; - usePhoneInput
import usePhoneInput from 'react-international-phone';
import { usePhoneInput } from 'react-international-phone'; - isValidPhoneNumber
import { validatePhone } from 'react-international-phone';import { isValidPhoneNumber } from 'react-international-phone'; - style.css
import 'react-international-phone/dist/style.css';
import 'react-international-phone/style.css';
Quickstart
import { useState } from 'react';
import { PhoneInput } from 'react-international-phone';
import 'react-international-phone/style.css';
const App = () => {
const [phone, setPhone] = useState('');
return (
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
minHeight: '100vh',
fontFamily: 'sans-serif'
}}>
<h1>Phone Number Input</h1>
<PhoneInput
defaultCountry="us"
value={phone}
onChange={(phone) => setPhone(phone)}
inputProps={{
name: 'phone',
placeholder: 'Enter phone number',
autoComplete: 'tel'
}}
className="my-custom-phone-input"
/>
<p style={{ marginTop: '20px' }}>Current Phone: {phone || 'None'}</p>
</div>
);
};
export default App;