zoukankan      html  css  js  c++  java
  • React 事件中 this 的三种使用方式

    1.bind绑定的方法、

    不传参

    class Hello extends React.Component{
        constructor(){
            super()
            this.state = {
                content:true
            }
            this.change = this.change.bind(this)
        }
        
        change(){
            this.setState({
                content:!this.state.content
            })
        }
    
        render(){
            return (
                <div>
                    <h1>{this.state.content ? '1':'2'}</h1>
                    <h2>{this.props.name}</h2>
                    <button onClick={this.change}>
                        点击
                    </button>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <Hello name="Hello"/>,
        document.getElementById('example')
    )

    传参

    class Hello extends React.Component{
        constructor(){
            super()
            this.state = {
                content:true
            }
            this.change = this.change.bind(this,this.state.content)
        }
        
        change(obj){
            this.setState({
                content:!this.state.content
            })
            console.log(obj)
            console.log(this.state.content)
        }
    
        render(){
            return (
                <div>
                    <h1>{this.state.content ? '1':'2'}</h1>
                    <h2>{this.props.name}</h2>
                    <button onClick={this.change}>
                        点击
                    </button>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <Hello name="Hello"/>,
        document.getElementById('example')
    )

    2.属性初始化器的方式

    不传参

    class Hello extends React.Component{
        constructor(){
            super()
            this.state = {
                content:true
            }
        }
        
        change=()=>{
            this.setState({
                content:!this.state.content
            })
        }
    
        render(){
            return (
                <div>
                    <h1>{this.state.content ? '1':'2'}</h1>
                    <h2>{this.props.name}</h2>
                    <button onClick={this.change}>
                        点击
                    </button>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <Hello name="Hello"/>,
        document.getElementById('example')
    )

    传参

    class Hello extends React.Component{
        constructor(){
            super()
            this.state = {
                content:true
            }
        }
        
        change=obj=>{
            this.setState({
                content:!this.state.content
            })
            console.log(obj)
        }
    
        render(){
            return (
                <div>
                    <h1>{this.state.content ? '1':'2'}</h1>
                    <h2>{this.props.name}</h2>
                    <button onClick={this.change(this.state.content)}>
                        点击
                    </button>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <Hello name="Hello"/>,
        document.getElementById('example')
    )

    3.回调函数的方式

    不传参

    class Hello extends React.Component{
        constructor(){
            super()
            this.state = {
                content:true
            }
        }
        
        change(){
            this.setState({
                content:!this.state.content
            })
        }
    
        render(){
            return (
                <div>
                    <h1>{this.state.content ? '1':'2'}</h1>
                    <h2>{this.props.name}</h2>
                    <button onClick={()=>{this.change()}}>
                        点击
                    </button>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <Hello name="Hello"/>,
        document.getElementById('example')
    )

    传参

    class Hello extends React.Component{
        constructor(){
            super()
            this.state = {
                content:true
            }
        }
        
        change(obj){
            this.setState({
                content:!this.state.content
            })
            console.log(obj)
        }
    
        render(){
            return (
                <div>
                    <h1>{this.state.content ? '1':'2'}</h1>
                    <h2>{this.props.name}</h2>
                    <button onClick={()=>{this.change(this.state.content)}}>
                        点击
                    </button>
                </div>
            )
        }
    }
    
    ReactDOM.render(
        <Hello name="Hello"/>,
        document.getElementById('example')
    )
  • 相关阅读:
    CGI与FastCGI
    Google Protocol Buffer 的使用和原理
    AMQP协议
    Java 多线程 并发编程
    深入理解HashMap
    Bitmap 位图
    Bloom Filter概念和原理
    BloomFilter–大规模数据处理利器
    java bitmap/bitvector的分析和应用
    Linux iptables 备忘
  • 原文地址:https://www.cnblogs.com/art-poet/p/12503948.html
Copyright © 2011-2022 走看看