Hey so I have a text box/FormControl, it should update a field in json in this.state. I want to know if there is a better way to change it?
type='text'
placeholder='enter'
defaultValue={this.state.form. name}
onChange={this.handleChange.bind(this,'name')}
/>
`
handleChange(change, event) {
var toChange = this.state.form;
toChange[change] = event.target.value;
this.setState ({form: toChange});
}
The optimized handleChange method is as follows. (will Replace’username’ with the field name you like…)
type='text'
name='username' < br /> placeholder='enter'
defaultValue={this.state.form.username}
onChange={this.handleChange.bind(this)}
/>
< /FormGroup>
handleChange(event) {
let fieldName = event.target.name;
let fleldVal = event.target.value;
this.setState( {form: {...this.state.form, [fieldName]: fleldVal}})
}
Hey so I have a text box/FormContro l, it should update a field in json in this.state. I want to know if there is a better way to change it?
type='text'
placeholder='enter'
defaultValue={this.state.form. name}
onChange={this.handleChange.bind(this,'name')}
/>
`
handleChange(change, event) {
var toChange = this.state.form;
toChange[change] = event.target.value;
this.setState ({form: toChange});
}
The optimized handleChange method is as follows. (Replace’username’ with the field name you like…)
type='text'
name='username'
placeholder='enter'
defaultValue={this.state.form.username}
onChange={this.handleChange.bind(this)}
/>
handleChange (event) {
let fieldName = event.target.name;
let fleldVal = event.target.value;
this.setState({form: {...this.state.form , [fieldName]: fleldVal}})
}