How to modify value in react

藏色散人
Release: 2022-12-21 16:13:00
Original
3124 people have browsed it

How to modify the value in react: 1. Open the corresponding front-end code file and get the parameters on the label; 2. Add a bind pointer to the place where the method is called; 3. Use bind to change the direction of this; 4. Use Just modify the value in the state state.

How to modify value in react

#The operating environment of this tutorial: Windows 10 system, react18 version, Dell G3 computer.

How to modify the value in react?

react component clicks to modify the value state

Changes the value on the label when clicked, but this cannot be obtained in the method, so the first thing to do is Just change this

class Leo extends React.Component{
            render(){
                return (<div>
                            <input type="button" value="点击" onClick={this.show}/>
                            <br/>
                            //span获取标签上的参数
                            <span>{this.props.a}</span>
                            <h1>sadfsdf</h1>
                        </div>)
            }
            show(){
                console.log(this)//在方法里如果直接调用this;打印出来会为Null,所以我们要做的就是改变this,需要在方法调用的地方加一个bind指向
            }
        }
        ReactDOM.render(<Leo a=&#39;12&#39;/>,app)
Copy after login

Use bind to change the direction of this

 class Leo extends React.Component{
            render(){
                return (<div>
                            <input type="button" value="点击" onClick={this.show.bind(this)}/>
                            <br/>
                            //span获取标签上的参数
                            <span>{this.props.a}</span>
                            <h1>sadfsdf</h1>
                        </div>)
            }
            show(){
                console.log(this)//在方法里如果直接调用this;打印出来会为Null,所以我们要做的就是改变this,需要在方法调用的地方加一个bind指向
            }
        }
        ReactDOM.render(<Leo a=&#39;12&#39;/>,app)
Copy after login

How to modify value in react

##Requirement: Modify the value in this.props.a

<script type="text/babel">
        class Leo extends React.Component{
            render(){
                return (<div>
                            <input type="button" value="点击" onClick={this.show.bind(this)}/>
                            <br/>
                            //span获取标签上的参数
                            <span>{this.props.a}</span>
                            <h1>sadfsdf</h1>
                        </div>)
            }
            show(){
               this.props.a = 12 //如果直接这样修改发面会报错,read only(只能读不能修改);如果要修改就应该用state状态
            }
        }
        ReactDOM.render(<Leo a=&#39;12&#39;/>,app)
    </script>
Copy after login

How to modify value in react

Use the state state to modify the value, you need to use the constructor to initialize

tip: props cannot change the value, but the state can be changed using setState

 <script type="text/babel">
        class Leo extends React.Component{
            constructor(){
                super();
                this.state = {
                    msg:&#39;hello react &#39;
                }
 
            }
            render(){
                return (<div>
                            <input type="button" value="点击" onClick={this.show.bind(this)}/>
                            <br/>
                            //hello react
                            <span>{this.state.msg}</span>
                            <h1>sadfsdf</h1>
                        </div>)
            }
            show(){
                this.setState({//点击修改span里的值
                    msg:&#39;哈哈&#39;
                })
            }
        }
        ReactDOM.render(<Leo/>,app)
    </script>
Copy after login

Recommended learning: "

react video tutorial"

The above is the detailed content of How to modify value 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!