Here is a solution with onBlur, it can be very helpful as it also allows you to format the number the way you need it without requiring any black magic or external library.
const toNumber = (value: string | number) => {
if (typeof value === 'number') return value
return parseInt(value.replace(/[^\d]+/g, ''))
}
const formatPrice = (price: string | number) => {
return new Intl.NumberFormat('es-PY').format(toNumber(price))
}
<input
defaultValue={formatPrice(price)}
onBlur={e => {
const numberValue = toNumber(e.target.value)
setPrice(numberValue)
e.target.value = formatPrice(numberValue)
}}
type='tel'
required
/>
defaultValue
Pay attention: In case your value come from a async source (e.g. fetch): Since defaultValue will only set the value on the first render, you need to make sure to render the component only once the data is there.