'React: input value disappears when other input fields change
I am new to React and still learning and I'm trying to debug someone else's component.
I have a form with a Date of Birth input field using InputMask as such:
<InputMask
mask="99/99/9999"
inputRef={(input) => this.state.DateOfBirth = input}
alwaysShowMask={true}
type="text"
id="contact-us-date-of-birth"
name="DateOfBirth"
required="true"
autoComplete="off"
value={this.state.DateOfBirth || ''}
onChange={this.handleChange}
/>
This works fine, except when the initial state for DateOfBirth is a preset. The preset state appears but then the value disappears whenever another field on the page changes (and triggers the handleChange handler).
The handleChange function simply sets the state for each form field and runs a validator:
handleChange(event) {
const currentTarget = event.currentTarget;
const value = currentTarget.value;
this.setState({
[currentTarget.name]: value
});
this.formValidator.handleChange(currentTarget);
}
Would anyone know why the above handler would cause the InputMask component to drop its value?
PS: other form field elements appear like:
<input
autoComplete="given-name"
id="FirstName"
maxLength="50"
name="FirstName"
pattern={this.validationConstants.Name}
required="true"
type="text"
value={this.state.FirstName || ''}
onChange={this.handleChange}
/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
