Home > Web Front-end > Front-end Q&A > How to modify the property value of an object in react

How to modify the property value of an object in react

藏色散人
Release: 2023-01-04 09:41:57
Original
3357 people have browsed it

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.

How to modify the property value of an object in react

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);
Copy after login

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)
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template