const [ amount, setAmount ] = useState(null || '');
import React, { useState } from 'react';
export function App(props) {
const [value, setValue] = useState( '');
const [bg, setBg] = useState('red');
const handleChange = event => {
setValue(event.target.value);
if(value.length < 5 || value.length>50){
setBg('red')
}
else{
setBg('')
}
};
return (
<div className='App'>
<input
type='text'
value={value}
onChange={handleChange}
style={{ backgroundColor: bg}}
/>
<pre>{value}</pre>
</div>
);
}
const Input = () => {
const range = { min: 5, max: 50 }
const [value, setValue] = useState<number | "">(range.min)
function onBlurHandle(value: number) {
if (value >= range.min && value < range.max) return;
setValue(range.min)
}
return (
<input
type="number"
value={value}
onChange={({ target }) => setValue(target.value === "" ? "" : +target.value)}
onBlur={({ target }) => onBlurHandle(+target.value)}
min={range.min}
max={range.max}
/>
)
}