zoukankan      html  css  js  c++  java
  • React之函数中的this指向

           我们都知道在React中使用函数时,有两种写法,一是回调函数,二是直接调用,但需要在构造函数中绑定this,只有这样,函数中的this才指向本组件

           总结一下没有绑定this的函数中的this指向

            不管是在本组件的元素上调用的函数还是传递给子组件的函数,不管有没有绑定this,它们调用的都是本组件里的函数,即调用函数的this指向的是本组件

             例如: class Parent extends React.Component {

                constructor(props) { supper(props); }

                 handleSubmit() {}

                  render() {

                      return (

                         <div>

                             <button onClick={this.handleSubmit}>点击按钮</button>

                             <Child  onFunc={this.handleSubmit} />

                         </div>

                      );

                  }

             }

            <button>与<Child />调用的都是Parent组件中的handleSubmit,不同的是handleSubmit函数中的this指向

            <button>的handleSubmit中的this为undefined

            <Child />的handleSubmit中的this指向Child组件的propTypes对象

  • 相关阅读:
    poj 2186 && hdu 3836
    poj 2833 The Average
    hud 3062 Party
    论 ACM 与泡妞 (转载)
    poj 1064 Cable master
    poj Candies
    [转]人才流失的背后
    获取存储过程的ReturnValue值
    javascript js jquery获取元素位置代码总结
    【引用】xmlpath 操作 xml
  • 原文地址:https://www.cnblogs.com/yezi-dream/p/9942425.html
Copyright © 2011-2022 走看看