0
I have a component and the following state:
this.state = {
form : {
name : '',
username: '',
email: '',
phone: '',
address: {
street: '',
suite: '',
city: '',
zipcode: ''
}
}
}
In form fields, use the following method in onChange:
handleChange(e) {
const {name, value} = e.target;
let copyState = Object.assign({}, this.state);
copyState.form[name] = value;
this.setState(copyState);
}
This is an example of a functional form field:
<input value={this.state.form.username}
onChange={this.handleChange}
name="username"
type="text"
className="form-control"
/>
This one doesn’t work:
<input value={this.state.form.address.street}
onChange={this.handleChange}
name="address.street"
type="text"
className="form-control"
/>
The handleChange method works perfectly on the items: name, username, email and phone, BUT not on the attributes belonging to the address.
How to solve this?
Thanks in advance. :)