React Currency Input Field
React Currency Input Field is a lightweight and feature-rich React component designed for formatting currency and numerical inputs within forms. Currently stable at version 4.0.5, it receives moderate updates, primarily focused on build stability and compatibility, as evidenced by recent patch releases. Key differentiators include support for numerical abbreviations (e.g., '1k'), customizable prefixes and suffixes, automatic group separators, and integration with the Internationalization API (Intl locale config) for region-specific formatting. It also provides keyboard stepping, allows/disallows decimals and negative values, and ships with full TypeScript support, all while maintaining zero runtime dependencies (beyond React itself) and a minimal bundle size.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'default') or 'CurrencyInput is not a function'
cause Attempting to `require` the module in a CommonJS environment, but it's primarily an ESM package with the default export nested under a `default` property.fixWhen using CommonJS `require`, destructure the default export: `const { default: CurrencyInput } = require('react-currency-input-field');` or migrate to ES modules: `import CurrencyInput from 'react-currency-input-field';` -
Module not found: Can't resolve 'react-currency-input-field'
cause The package is not correctly installed, or your build tool/bundler cannot locate the module.fixEnsure the package is installed: `npm install react-currency-input-field` or `yarn add react-currency-input-field`. Verify your bundler's module resolution paths and aliases. For SSR, ensure you are on `v4.0.3` or newer for better ESM/CJS compatibility. -
Invariant Violation: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.
cause Mixing `defaultValue` and `value` props, or conditionally rendering `value` such that it transitions from `undefined` to a defined state.fixChoose either `defaultValue` (uncontrolled) or `value` (controlled with `onValueChange`) for the entire lifecycle of the component. If `value` is used, ensure it is always defined or explicitly `null` if no value is present, rather than switching between `undefined` and defined.
Warnings
- breaking Version 4.0.0 introduced a switch from Rollup to esbuild for the build process. While the public API remained largely consistent, this change can impact projects relying on specific module resolution behaviors or build configurations.
- gotcha Between v4.0.0 and v4.0.3, there were known issues with Node.js ESM resolution in Server-Side Rendering (SSR) environments. This could lead to modules not being found or incorrectly resolved.
- gotcha Incorrect usage of `defaultValue` and `value` props can lead to an input becoming either uncontrolled or a 'controlled component' error in React. `defaultValue` is for uncontrolled inputs, while `value` should be used with `onValueChange` for controlled inputs.
Install
-
npm install react-currency-input-field -
yarn add react-currency-input-field -
pnpm add react-currency-input-field
Imports
- CurrencyInput
import { CurrencyInput } from 'react-currency-input-field'; const CurrencyInput = require('react-currency-input-field');import CurrencyInput from 'react-currency-input-field';
- CurrencyInputProps
import type { CurrencyInputProps } from 'react-currency-input-field'; - All symbols (CommonJS)
const CurrencyInput = require('react-currency-input-field');const { default: CurrencyInput } = require('react-currency-input-field');
Quickstart
import React, { useState } from 'react';
import CurrencyInput from 'react-currency-input-field';
const MyCurrencyForm = () => {
const [value, setValue] = useState<string | undefined>('1234.56');
return (
<div>
<label htmlFor="currency-input">Enter Amount:</label>
<CurrencyInput
id="currency-input"
name="currency-field"
placeholder="Please enter an amount"
defaultValue={1234.56}
decimalsLimit={2}
prefix="$"
onValueChange={(value, name, values) => {
setValue(value);
console.log(`Value: ${value}, Name: ${name}, Values:`, values);
}}
value={value} // Use value for controlled component behavior
/>
<p>Current Input: {value}</p>
</div>
);
};
export default MyCurrencyForm;