Home > Article > Web Front-end > How to modify the property value of an object in react
React method to modify object attribute values: 1. Open the corresponding react code file; 2. View "this.setState({message:event.target.value})"; 3. Pass "let data= Object.assign({}, this.state.datavalue, {name: val})this.setState({datavalue: data})” to modify the attribute value.
The operating environment of this tutorial: Windows 10 system, react18.0.0 version, Dell G3 computer.
How to modify the attribute value of an object in react?
react Modifies the state to a certain attribute value in the object
Use the assign method of the object
The Object.assign method is used to merge objects and assign the source object All enumerable properties of (source) are copied to the target object (target)
The first parameter of the Object.assign method is the target object, and the subsequent parameters are all source objects.
If the target object and the source object have attributes with the same name, or multiple source objects have attributes with the same name, the later attributes will overwrite the previous attributes.
react Modify state to a certain attribute value in the object
var NoLink = React.createClass({ getInitialState:function(){ return {message:''} }, handelChange:function(event){ console.log(event.target); this.setState({message:event.target.value}) }, render:function(){ var mess = this.state.message; return ( <div> <input type="text" onChange={this.handelChange} value={mess} /> <b>{mess}</b> </div> ) } }); React.render(<NoLink />,document.body);
Modify the value of the object below state
SetName = (e) = > { let val = e.target.value; let data = Object.assign({}, this.state.datavalue, { name: val }) this.setState({ datavalue: data }) console.log(this.state.datavalue, data) }
Recommended learning: "react video tutorial 》
The above is the detailed content of How to modify the property value of an object in react. For more information, please follow other related articles on the PHP Chinese website!