上接:https://www.cnblogs.com/chenxi188/p/11782349.html

项目目录:

my-app/
README.md
node_modules/
package.json
.gitignore
public/
favicon.ico
index.html
manifest.json
src/
componets/
Demo.js
App.css
App.js
App.test.js
index.css
index.js
logo.svg

一、自写一个方法(函数),然后用按钮onClick触发它

如果想要在react模板render()内执行一个自定方法(函数),就要这样写,在components目录下新建立一个【Demo.js】:

import React from 'react';

class Demo extends React.Component{
constructor(){
super(); this.state={
name:'xiaoming'
}
} //自定义方法要写在构造函数外
run2(){
alert('这是一个待执行函数')
} render(){
return(
<div>
<button onClick={this.run2}>点这里执行函数</button>
</div>
);
}
} export default Demo;

【App.js】

import React, { Component } from 'react';
import logo from './logo.svg';
//import './App.css'; //从components文件夹下引入js
import Demo from './components/Demo.js'; class App extends Component { render (){
return(
<div className="App">
<h1>这里是app.js根组件</h1> <Demo />
</div>
);}
} export default App;

进入项目根目录:运行cmd:npm start,运行结果:

二、自写方法引用构造函数内的数据(改变this指向的)写法

写法1【Demo.js】:

import React from 'react';

class Demo extends React.Component{
constructor(){
super(); this.state={
msg:'this is a state messages!'
}
} //自定义方法:引用构造函数内的数据写法1(引用时必须.bind(this),否则报错,找不到msg):
run2(){
alert(this.state.msg)
} render(){
return(
<div>
<button onClick={this.run2.bind(this)}>点这里执行函数</button>
</div>
);
}
} export default Demo;

写法2【Demo.js】:

import React from 'react';

class Demo extends React.Component{
constructor(){
super(); this.state={
msg:'方法2 成功获取到我咯——state信息!'
} //第2种获取构造函数state内数据写法【在构造函数内提前用.bind(this),声明this.getmsg()
this.getmsg=this.getmsg.bind(this);
} //自定义方法:引用构造函数内的数据写法2(引用时):
getmsg(){
alert(this.state.msg)
} render(){
return(
<div>
<button onClick={this.getmsg}>点这里执行函数</button>
</div>
);
}
} export default Demo;

写法3:【Demo.js】最省事写法

import React from 'react';

class Demo extends React.Component{
constructor(){
super(); this.state={
msg:'方法3 成功获取到我咯——state信息!'
}
} //自定义方法 引用构造函数内的数据写法3,不必用.bind(this):
getmsg=()=>{
alert(this.state.msg)
} render(){
return(
<div>
<button onClick={this.getmsg}>点这里执行函数</button>
</div>
);
}
} export default Demo;

不用.bind(this)时报错,因为它无法正确指向

App.js用不变,直接刷新网址查看效果:

三、改变state值的写法this.setState()

import React from 'react';

class Demo extends React.Component{
constructor(){
super(); this.state={
msg:'这是state的msg原始值'
}
} //用setState()方法重写state指定值(这里用方法3,箭头函数改变this指向方式)
changeMsg=()=>{
this.setState({
msg:"!!!!!!这是state的msg改变值!!!!!"
})
} render(){
return(
<div>
<h1>{this.state.msg}</h1>
<button onClick={this.changeMsg}>点这里改变state值</button>
</div>
);
}
} export default Demo;

运行结果:

点击后:

四、通过自写方法的参数,向state内传值

【1】pushMsg=(name)=>{this.setState({            msg:name        })
【2】onClick={this.pushMsg.bind(this,'我是用参数传过来的值')}

4.1传一个参数【Demo.js】:
import React from 'react';

class Demo extends React.Component{
constructor(props){
super(props); this.state={
msg:'原始值'
}
}
//【1】自写带参数函数
pushMsg=(name)=>{
this.setState({
msg:name
})
} render(){
return(
<div>
<h1>{this.state.msg}</h1>
{
//【2】调用自写函数用参数传值到state
}
<button onClick={this.pushMsg.bind(this,'我是用参数传过来的值')}>点这里用方法传值</button>
</div>
)
}
} export default Demo;

App.js:

import React from 'react';
import logo from './logo.svg';
import './App.css';
import Demo from './components/Demo' function App() {
return (
<div className="App">
<img src={logo} className="App-logo" alt="logo" /> <Demo />
</div>
);
} export default App;

【效果】

点后:

4.2通过自写函数传多个参数值到state

Demo.js

import React from 'react';

class Demo extends React.Component{
constructor(props){
super(props); this.state={
msg:'原始值',
msg2:'原始值2号'
}
}
//自写带参数函数
pushMsg=(name,name2)=>{
this.setState({
msg:name,
msg2:name2
})
} render(){
return(
<div>
<h1>{this.state.msg}</h1>
<h1>{this.state.msg2}</h1>
{
//调用自写函数用参数传值到state
}
<button onClick={this.pushMsg.bind(this,'我是用参数传过来的值。','传过来第二个值')}>点这里用方法传值</button>
</div>
)
}
} export default Demo;

App.js不变

效果:

点后:

五、React事件方法(自写一个方法(函数),然后用按钮onClick触发它、自写方法改变this指向3种写法、的更多相关文章

  1. 请写一个php函数,可以接受任意数量的参数

    请写一个php函数,可以接受任意数量的参数 这是一道面试题.怎么写这个函数呢? function fun(......) { } ----------------------------------- ...

  2. 自己写一个strcmp函数(C++)

    题目说明: 写一个函数,实现两个字符串的比较.即自己写一个strcmp函数,函数原型为int strcmp( char * p1, char * p2); 设p1指向字符串s1,p2指向字符串s2.要 ...

  3. 写一个PHP函数,实现扫描并打印出指定目录下(含子目录)的所有jpg文件名

    写一个PHP函数,实现扫描并打印出指定目录下(含子目录)的所有jpg文件名 <?php $dir = "E:\照片\\";//打印文件夹中所有jpg文件 function p ...

  4. 【Java面试题】23 java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用?

    java5 以前, 有如下两种:第一种:new Thread(){}.start();这表示调用 Thread 子类对象的 run 方法, new Thread(){}表示一个Thread 的匿名子类 ...

  5. JavaScript 自己写一个 replaceAll() 函数

    JavaScript 的  replace()  方法可以在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 但是,只输入字符串的话,仅替换第一个字符,当然也可以用正则表达式来进行 ...

  6. 依据ECMA规范,手写一个bind函数

    Function.prototype.bind 函数,参见ECMA规范地址 如题,这次来实现一个boundFunction函数,不挂载在Function.prototype上,而是一个单独声明的函数. ...

  7. 为LoadRunner写一个lr_save_float函数

    LoadRunner中有lr_save_int() 和lr_save_string() 函数,但是没有保存浮点数到变量的lr_save_float函数.<lr_save_float() func ...

  8. 【写一个自己的js库】 5.添加修改样式的方法

    1.根据id或class或tag修改样式,样式名是-连接格式的. function setStyleById(elem, styles){ if(!(elem = $(elem)) return fa ...

  9. 34.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然 后写一个类Print实现接口InterfaceA和InterfaceB,要求printCapitalLetter()方法 实现输出大写英文字母表的功能,

    //接口InterfaceA package jieKou; public interface IInterfaceA { void printCapitalLetter(); } //接口Inter ...

随机推荐

  1. Django线上项目后台admin不显示问题

    记一次django项目的线上部署错误,django+nginx 一.问题描述 在将django项目部署到阿里云(nginx作为web服务器)上之后发现出现后台管理界面admin不显示样式. 二.问题分 ...

  2. 一 注册功能&登录功能,权限拦截

    注册功能: 前端JSP:提供表单注册信息以及访问路径,发送请求到Strus2. Struts2 : 通过模型驱动接收并封装User对象,Spring依赖注入(无参构造+setter方法)获取业务层Us ...

  3. storm的JavaAPI运行报错

    报错:java.lang.NoClassDefFoundError: org/apache/storm/topology/IRichSpout 原因:idea的bug:本地运行时设置scope为pro ...

  4. 转:Nginx的超时keeplive_timeout配置详解

    https://blog.csdn.net/weixin_42350212/article/details/81123932 Nginx 处理的每个请求均有相应的超时设置.如果做好这些超时时间的限定, ...

  5. struts的错误回显

  6. 安装lnmp1.5到最后出现Error: MySQL install failed的解决方法

    解决方法: mv /usr/bin/cmake /usr/bin/cmake.backup wget http://www.cmake.org/files/v3.0/cmake-3.0.2.tar.g ...

  7. 【Go语言系列】2.2、Go语言基本程序结构:关键字与标识符

    什么是标识符 标识符用来命名变量.类型等程序实体.标识符是指Go语言对各种变量.方法.函数等命名时使用的字符序列,标识符由若干个字母.下划线_.和数字组成,且第一个字符必须是字母.通俗的讲就是凡可以自 ...

  8. 提升Essay写作质量,可从这三个层次入手

    “有针对性”读书对写Essay的产生的帮助是非常大的.由浅显的直接成效,到深度的铺垫积累,阅读一共可分为三个层次: 1读Essay: 2读与写作题材相关的材料: 3多样化阅读. 第一层次:读Essay ...

  9. ROS 命令行工具的使用

    1.roscore 打开一个新的master(master:进程),只能运行一个,运行两个会报错,使用ROS第一步就是要打开roscore 2.rosrun rosrun的使用格式一般为:rosrun ...

  10. POJ 1979 Red and Black 四方向棋盘搜索

    Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 50913   Accepted: 27001 D ...