I have a controlled component with an input
field representing a day value. Since this day value can have 1-2 digits, e.g. one has to to delete 12 to enter 21.
Since the onChange
react-event behaves like the input
DOM-event, I can only delete or enter one digit and the event is fired, hence the whole model gets updated too early with day set to one after I deleted a digit.
<input
name={name}
type="number"
value={value}
onChange={(e) => { onChange(e.target.value) } }
/>
Thanks to defaultValue change does not re-render input I can handle this with an uncontrolled component input with an onBlur
event. Using key
ensures, that a new rendering happens, when the model is changed by other means:
<input
name={name}
type="number"
defaultValue={value}
key={value}
onBlur={(e) => { onChange(e.target.value); }}
/>
But honestly, this feels like a hack. How do you pros solve this scenario? Have I overlooked something simpler? Do you use a timeout-function before updating the model (thus waiting for complete user-input) aka debounce?