1.使用bind

<button onClick={this.test.bind(this)}>确定</button>

也可以这么写:

<button onClick={this.test}>确定</button>

在constructor里把bind赋值给方法

 constructor(props){
super(props);
this.state= {
haha:"123"
}
this.test = this.test.bind(this);
}

2.使用箭头函数

 <button onClick={()=>this.test2()}>确定</button>

也可以这么写

 <button  onClick={this.test2}>确定</button>
  test2=()=>{
console.log(222);
console.log(this.state.haha);
}

react添加方法的两种形式的更多相关文章

  1. js数组常用添加方法有两种

        //头部     //this.list.unshift({name:this.itemName,date:new Date()});     //尾部         this.list.p ...

  2. Linux添加系统调用的两种方法

    前言 系统调用的基本原理 系统调用其实就是函数调用,只不过调用的是内核态的函数,但是我们知道,用户态是不能随意调用内核态的函数的,所以采用软中断的方式从用户态陷入到内核态.在内核中通过软中断0X80, ...

  3. SQL 关于apply的两种形式cross apply 和 outer apply(转)

    转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...

  4. SQL关于apply的两种形式cross apply和outer apply(转载)

    SQL 关于apply的两种形式cross apply 和 outer apply   apply有两种形式: cross apply 和 outer apply   先看看语法:   <lef ...

  5. Controller@实现Controller的两种形式

    实现Controller的两种形式 形式1:仅仅实现IController接口,自定义Controller对Request的实现.形式2:在实现IController接口以后,继承Controller ...

  6. React组件导入的两种方式(动态导入组件的实现)

    一. react组件两种导入方式 React组件可以通过两种方式导入另一个组件 import(常用) import component from './component' require const ...

  7. 转:SQL 关于apply的两种形式cross apply 和 outer apply

    原文地址:http://www.cnblogs.com/Leo_wl/archive/2013/04/02/2997012.html SQL 关于apply的两种形式cross apply 和 out ...

  8. SQL 关于apply的两种形式cross apply 和 outer apply, with cube 、with rollup 和 grouping

    1). apply有两种形式: cross apply 和 outer apply先看看语法: <left_table_expression> {cross|outer} apply &l ...

  9. MyBatis collection的两种形式——MyBatis学习笔记之九

    与association一样,collection元素也有两种形式,现介绍如下: 一.嵌套的resultMap 实际上以前的示例使用的就是这种方法,今天介绍它的另一种写法.还是以教师映射为例,修改映射 ...

随机推荐

  1. sql case 与 sum

    <select id="selectTotal" resultType="java.util.Map" parameterType="java. ...

  2. 项目总结13:Jav文件压缩-InputStream转化为base64-Base64解码并生成图片

    Jav文件压缩-InputStream转化为base64-Base64解码并生成图片 直接上源码,解释见文章尾部 package com.hs.common.util.imgecode; import ...

  3. xcode10 - 打ipa上蒲公英或者fire.im

    1.选择空设备 2. 3. 4. 选择需要的 next 5. 6. 7. 8.选择位置 9. 选择ipa包 放到蒲公英 或者fire.im上 就行了

  4. [leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  5. Java10-java语法基础(九)——java的封装性

    Java10-java语法基础(九)——java的封装性 一.Java的三大特性:封装.多态.继承 封装:通过类封装对象的数据成员和成员方法,保证只有可信的类或者对象能够访问这些方法和数据成员,对不可 ...

  6. 2Q - Fibbonacci Number

    Your objective for this question is to develop a program which will generate a fibbonacci number. Th ...

  7. Executors提供的四种线程池

    Java 5+中的Executor接口定义一个执行线程的工具.它的子类型即线程池接口是ExecutorService.要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,因此在工具 ...

  8. BFS和DFS (java版)

    package com.algorithm.test; import java.util.ArrayDeque; import java.util.Scanner; public class DfsA ...

  9. 20172325 2018-2019-2 《Java程序设计》第五周学习总结

    20172325 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 本次学习第九章内容,主要学习查找和排序. 查找 查找的定义:是一个过程,即在某个项目组中寻找 ...

  10. python 正则表达式 group() groups()

    参考地址: http://www.cnblogs.com/kaituorensheng/archive/2012/08/20/2648209.html