美文网首页
react 事件关系,四种this指定事件

react 事件关系,四种this指定事件

作者: 小李不小 | 来源:发表于2020-08-03 22:29 被阅读0次

this 指向一共有四种方法

点击事件的四种方法-箭头函数
         <button onClick={this.updateUsertwo}>点击事件的四种方法-箭头函数</button>
updateUsertwo=()=>{
        console.log('点击事件的四种方法-箭头函数',this)
    }

点击事件的四种方法-函数内容写方法
            <button onClick={()=>this.updateUsertwop()}>点击事件的四种方法-函数内容写方法</button>
    updateUsertwop(){
        console.log('u点击事件的四种方法-函数内容写方法',this)
    }
击事件的四种方法-方法内绑定bind-this
<button onClick={()=>this.updateUsertwop()}>点击事件的四种方法-函数内容写方法</button>
    updateUsertwoa(){
        console.log('点击事件的四种方法-方法内绑定bind-this',this)
    }

点击事件的四种方法-箭头函数--初始化bind绑定this<

constructor(props){
    //初始化状态 bind this
    this.updateUsertwos=this.updateUsertwos.bind(this);
}
<button onClick={this.updateUsertwos}>点击事件的四种方法-箭头函数--初始化bind绑定this</button>
    updateUsertwos(){
        console.log('点击事件的四种方法-箭头函数--初始化bind绑定this',this)
    }
<div id="example"></div>

    render(){
        console.log('组件的加载和数据的更新------render')
        return <div>
            <h1>hello ,{this.state.name}</h1>
            <p>年龄,{this.state.age}</p>
            <p>擅长,{this.state.npg}</p>
            <br/>
            <button onClick={this.updateUser}>更新事件</button>
            <br/>
            <button onClick={this.updateUsertwo}>点击事件的四种方法-箭头函数</button>
            <br/>
            <button onClick={()=>this.updateUsertwop()}>点击事件的四种方法-函数内容写方法</button>
            <br/>
            <button onClick={this.updateUsertwoa.bind(this)}>点击事件的四种方法-方法内绑定bind-this</button>
            <br/>
            <button onClick={this.updateUsertwos}>点击事件的四种方法-箭头函数--初始化bind绑定this</button>
        </div>
    }


    //箭头函数
    updateUsertwo=()=>{
        console.log('点击事件的四种方法-箭头函数',this)
    }

    updateUsertwop(){
        console.log('u点击事件的四种方法-函数内容写方法',this)
    }

    updateUsertwoa(){
        console.log('点击事件的四种方法-方法内绑定bind-this',this)
    }

    updateUsertwos(){
        console.log('点击事件的四种方法-箭头函数--初始化bind绑定this',this)
    }


ReactDOM.render(
    <Hello></Hello>,
    document.getElementById('example')
    )


image.png

相关文章

  • react 事件关系,四种this指定事件

    this 指向一共有四种方法 点击事件的四种方法-箭头函数 点击事件的四种方法-函数内容写方法 击事件的四种方法-...

  • 【React进阶系列】史上最全React事件机制详解

    框架总览 ? DOM事件流的三个阶段 ? 关于React事件的疑问? React事件绑定机制? React事件和原...

  • 2018-11-07 react 事件处理

    react事件处理和dom事件处理是相似的。 react: Dom: 所以: React事件绑定属性的命名采用驼峰...

  • 【React深入】React事件机制

    关于React事件的疑问 1.为什么要手动绑定this 2.React事件和原生事件有什么区别 3.React事件...

  • react的事件处理

    一. 通过onXXXX属性指定事件处理函数(注意大小写) 1.React使用的其实是自定义事件,并非单纯的onXX...

  • react学习(19)事件处理

    知识点 1:通过onXxx属性指定事件处理函数(注意大小写) a:react中使用的是自定义合成事件,为了更好的兼...

  • React基础(6) -- 事件处理

    React 事件处理 React 元素的事件处理和 DOM 元素类似。但是有一点语法上的不同: React 事件绑...

  • ReactJS_06 React 事件处理

    React 事件处理 React 元素的事件处理和 DOM 元素类似。但是有一点语法上的不同: React 事件绑...

  • React 事件处理

    React 事件处理 React 元素的事件处理和 DOM 元素类似。但是有一点语法上的不同: React 事件绑...

  • React 事件

    React 事件 React 自己维护了自己的事件, 已经对事件进行封装解决了浏览器兼容的问题 React 使用了...

网友评论

      本文标题:react 事件关系,四种this指定事件

      本文链接:https://www.haomeiwen.com/subject/nlzbrktx.html