First, the manner your are changing the state is not good. You are setting the new state to have only a single attribute which is the name and value of the field which is modified.
Coming to how to print the value of the field you are modifying, you should do a console log in the handle change method.
That said, your code should look like:
handleInputChange(event) {
//const {name, value} = event.target;
//const oldState = this.state;
//const newState = Object.assign({},oldState,{[name]:value});
//this.setState(newState);
this.setState({
[event.target.name]: event.target.value
});
//printing the input name and value as it is being modified.
console.log(name + ":" + value);
//if you want to see the value of the current state uncomment the line below
//console.log("State=" + JSON.stringify(newState));
}
printState = () => {
console.log("State="+JSON.stringify(this.state));
}
For more insight on how to modify objects with assign method see: MDN doc on Object.assign()
If you want to print the current state on clicking the save button then add the onClick attribute to the button as shown in the code below:
<button value="Save" onClick={this.printState}/>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…