I've got a form that looks like:
At the bottom is the relevant part of the form state, cast to JSON, which is in this component.
When you click 'Add another', it adds another pair of fields. These field pairs are a child component, CountryFieldRow
.
In the child component, when the fields are edited, the child's state is updated properly.
How do I get the state to update in the parent object, so I can update the JSON printed?
Edit, code added:
import React, { Component } from 'react';
import CountryFormRow from "./CountryFormRow"
class CountryForm extends React.Component {
constructor(props) {
super(props)
this.state = {rules: props.rules}
this.handleDelete = this.handleDelete.bind(this)
this.handleAdd = this.handleAdd.bind(this)
this.handleInputChange = this.handleInputChange.bind(this)
}
handleInputChange(event) {
console.log(event)
this.setState({
[event.target.name]: event.target.value
});
}
handleDelete(event) {
this.setState({rules: this.state.rules.slice(0, -1)})
console.log(rules)
}
handleAdd(even) {
this.setState({rules: this.state.rules.concat({what: "country"})})
}
handleChange(event){
console.log(event)
}
render() {
// var my_json = JSON.stringify(this.state.rules)
// console.log(my_json)
var controls = this.state.rules.map((rule,index)=>
<CountryFormRow key={index} id={rule.id} url={rule.url} matches={rule.matches} what={rule.what} onUpdate={this.handleChange} />
)
var buttons =
<div className="form-group row">
<div className="col-sm-12">
<div className="pull-right">
{this.state.rules.length > 0 &&
<a onClick={this.handleDelete} className="btn btn-white btn-sm">
Remove
</a>
}
<a onClick={this.handleAdd} className="btn btn-white btn-sm">
+ Add Another
</a>
</div>
</div>
</div>
var json_state =
<div>
{JSON.stringify(this.state.rules)}
</div>
return([controls, buttons, json_state])
}
}
export default CountryForm;
Child object:
import React, { Component } from 'react';
class CountryFormRow extends React.Component {
constructor(props) {
super(props)
this.state = {url: props.url, matches: props.matches, what: props.what}
this.handleInputChange = this.handleInputChange.bind(this)
}
handleInputChange(event) {
this.setState({
[event.target.name]: event.target.value
});
}
render() {
return (
<div className="geo-field">
<div className="form-group row">
<input id="link_dd_0_what" name="link[dd_0_what]" type="hidden" value="this.props.country"/>
<label className="col-sm-2 col-form-label">Country</label>
<div className="col-sm-10">
<select onChange={this.handleInputChange} className="form-control m-b" name="matches" defaultValue={this.props.matches}><option value=""></option><option value="AF">Afghanistan</option><option value="AL">Albania</option><option value="DZ">Algeria</option><option value="AS">American Samoa</option></select>
</div>
</div>
<div className="form-group row">
<label className="col-sm-2 col-form-label">Destination</label>
<div className="col-sm-10">
<input onChange={this.handleInputChange} className="form-control" name="url" placeholder="https://www.example.com" type="text" defaultValue={this.props.url}/>
</div>
</div>
</div>
)
}
}
export default CountryFormRow