Masked Input Component for React
react-input-mask provides a simple and effective masked input component for React applications, allowing developers to enforce specific input formats such as phone numbers, dates, or social security numbers. The current stable version is 2.0.4, which was released approximately six years ago. The project appears to be in maintenance mode, with no significant updates since then. Its primary differentiator is its focused scope and direct integration into React workflows, offering a straightforward API for controlling input masking behavior and supporting dynamic mask changes. It's widely adopted for basic input masking requirements in React.
Common errors
-
Warning: A component is changing an uncontrolled input of type text to be controlled.
cause This typically happens in React when an input component switches between being controlled (having a `value` prop) and uncontrolled (not having a `value` prop, or having `defaultValue`). Ensure that the `value` prop is always provided and managed by React state.fixAlways initialize the state managing the input's value, even if it's an empty string, and ensure the `onChange` handler correctly updates this state. For `react-input-mask`, ensure its `value` prop is consistently bound to a state variable. -
Property 'mask' does not exist on type 'IntrinsicAttributes & InputMaskProps & { children?: ReactNode; }'.cause This TypeScript error occurs when `InputMask` is imported incorrectly, often as a named import (e.g., `{ InputMask }`) when it is a default export.fixChange your import statement to `import InputMask from 'react-input-mask';` to correctly import the default component.
Warnings
- breaking Version 2.0.0 dropped support for React 0.13. Projects on older React versions must upgrade React before migrating to `react-input-mask` v2.
- breaking Older versions of `react-input-mask` might exhibit compatibility issues or warnings with React StrictMode (introduced in React 16.3) due to reliance on deprecated lifecycle methods like `componentWillReceiveProps`. Version 2.0.0 addressed these issues.
- gotcha Using the `maxLength` prop directly on the `InputMask` component can lead to incorrect behavior or console warnings. The mask itself dictates the effective maximum length.
- gotcha The project appears to be in maintenance mode, with the last release (v2.0.4) being several years old. While stable, new features or active bug fixes are unlikely.
Install
-
npm install react-input-mask -
yarn add react-input-mask -
pnpm add react-input-mask
Imports
- InputMask
import { InputMask } from 'react-input-mask';import InputMask from 'react-input-mask';
- InputMask (CJS)
const InputMask = require('react-input-mask');
Quickstart
import React, { useState } from 'react';
import InputMask from 'react-input-mask';
function PhoneInput() {
const [phone, setPhone] = useState('');
const handleChange = (e) => {
setPhone(e.target.value);
};
return (
<div>
<label htmlFor="phone-number">Phone Number:</label>
<InputMask
mask="+1 (999) 999-9999"
value={phone}
onChange={handleChange}
maskChar="_"
placeholder="+1 (___) ___-____"
id="phone-number"
className="form-input"
>
{(inputProps) => <input {...inputProps} type="tel" />}
</InputMask>
<p>Current Value: {phone}</p>
</div>
);
}
export default PhoneInput;