To support controlled inputs, @react-input/number-format does not modify the value passed to the input element. This means the state value must exactly match the formatted value displayed in the input.
If your state contains unformatted numbers, use the format utility to convert the value into the correct formatted string before passing it to the value prop. Failure to do so will result in a console warning.
For uncontrolled inputs, the library handles formatting automatically upon initialization.
import { useState } from 'react';
import { useNumberFormat, format } from '@react-input/number-format';
const options = {
locales: 'en',
maximumFractionDigits: 2,
};
export default function App() {
const inputRef = useNumberFormat(options);
// Use format() to ensure the initial state matches the expected formatted string
const defaultValue = format(123456789, options);
const [value, setValue] = useState(defaultValue);
return <input ref={inputRef} value={value} onChange={(event) => setValue(event.target.value)} />;
}