leiwuhen-67's blog leiwuhen-67's blog
首页
    • 《Vue》笔记
    • 《React》笔记
    • 《NodeJs》笔记
    • 《CSS》笔记
    • 《Redis》笔记
    • 基础入门
    • 《Mock》笔记
    • 《MySQL》笔记
    • 《Git》相关
影音视听
收藏
关于
GitHub (opens new window)

我的公众号

首页
    • 《Vue》笔记
    • 《React》笔记
    • 《NodeJs》笔记
    • 《CSS》笔记
    • 《Redis》笔记
    • 基础入门
    • 《Mock》笔记
    • 《MySQL》笔记
    • 《Git》相关
影音视听
收藏
关于
GitHub (opens new window)
  • React

    • 使用React脚手架快速搭建项目
    • hooks之useEffect
    • React之事件绑定及简写方式
    • React之props传值并对其进行限制
    • React之Refs的基本使用
    • React之生命周期钩子
    • React之key的使用
    • React之代理服务器配置
    • React之封装全局Loading组件
    • React之消息的发布-订阅(pubsub-js)
    • React之Redux的基本使用
    • React之react-redux的基本使用
    • React之redux的数据持久化存储
    • React之路由懒加载
    • React之Render Props的使用
    • React之createBrowserRouter
    • React之路径别名@配置
    • React之craco打包优化配置
    • React之项目国际化
    • React之postcss-pxtorem移动端适配
    • React之使用vite创建项目
    • React之ts类型标注汇总
    • React之使用 ant design搭建后台管理之踩坑
    • React之路由切换动画
    • React之使用vite创建组件库并发布到npm
    • React之项目打包部署到nginx
    • React之自定义组件添加className与style
  • React Native

  • 《React》笔记
  • React
心欲无痕
2024-03-07

React之事件绑定及简写方式

在 React 中绑定事件有以下几种方式:

①:在构造函数中使用 bind 绑定 this:

class App extends React.Component {
	constructor (props) {
		super(props)
		this.state = {
			btnTxt: "保存"
		}
		this.changeBtnTxt = this.changeBtnTxt.bind(this)
	}
	changeBtnTxt () {
		this.setState({
			btnTxt: this.state.btnTxt === "保存" ? "编辑" : "保存"
		})
	}
	render () {
		return (
			<div>
				<button onClick = { this.changeBtnTxt }>
					{ this.state.btnTxt }
				</button>
			</div>
		)
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

②:在调用时使用 bind 绑定 this:

class App extends React.Component {
	constructor (props) {
		super(props)
		this.state = {
			btnTxt: "保存"
		}
	}
	changeBtnTxt () {
		this.setState({
			btnTxt: this.state.btnTxt === "保存" ? "编辑" : "保存"
		})
	}
	render () {
		return (
			<div>
				<button onClick = { this.changeBtnTxt.bind(this) }>
					{ this.state.btnTxt }
				</button>
			</div>
		)
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

③:在调用时使用箭头函数绑定 this:

class App extends React.Component {
	constructor (props) {
		super(props)
		this.state = {
			btnTxt: "保存"
		}
	}
	changeBtnTxt () {
		this.setState({
			btnTxt: this.state.btnTxt === "保存" ? "编辑" : "保存"
		})
	}
	render () {
		return (
			<div>
				<button onClick = { () => this.changeBtnTxt() }>
					{ this.state.btnTxt }
				</button>
			</div>
		)
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

④:使用属性初始化器语法绑定 this:

class App extends React.Component {
	constructor (props) {
		super(props)
		this.state = {
			btnTxt: "保存"
		}
	}
	changeBtnTxt = () => {
		this.setState({
			btnTxt: this.state.btnTxt === "保存" ? "编辑" : "保存"
		})
	}
	render () {
		return (
			<div>
				<button onClick = { this.changeBtnTxt }>
					{ this.state.btnTxt }
				</button>
			</div>
		)
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

比较:

方式 2 和方式 3 都是在调用的时候再绑定 this。

  • 优点:写法比较简单,当组件中没有 state 的时候就不需要添加类构造函数来绑定 this
  • 缺点:每一次调用的时候都会生成一个新的方法实例,因此对性能有影响,并且当这个函数作为属性值传入低阶组件的时候,这些组件可能会进行额外的重新渲染,因为每一次都是新的方法实例作为的新的属性传递。

方式 1 在类构造函数中绑定 this,调用的时候不需要再绑定

  • 优点:只会生成一个方法实例,并且绑定一次之后如果多次用到这个方法也不需要再绑定。
  • 缺点:即使不用到 state,也需要添加类构造函数来绑定 this,代码量多一点。。。

方式 4:利用属性初始化语法,将方法初始化为箭头函数,因此在创建函数的时候就绑定了 this。

  • 优点:创建方法就绑定 this,不需要在类构造函数中绑定,调用的时候不需要再作绑定。结合了方式 1、方式 2、方式 3 的优点
  • 缺点:需要用 babel 转译

简写方式(推荐,虽然需要 babel 进行转译,但是在使用 Create React App 开发时,默认是启用这种语法的):

class App extends React.Component {
	// 表示给App组件实例添加一个state属性,值为一个对象
	state = {
		btnTxt: "保存"
	}
	changeBtnTxt = () => {
		this.setState({
			btnTxt: this.state.btnTxt === "保存" ? "编辑" : "保存"
		})
	}
	render () {
		return (
			<div>
				<button onClick = { this.changeBtnTxt }>
					{ this.state.btnTxt }
				</button>
			</div>
		)
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
编辑 (opens new window)
上次更新: 7/1/2024, 4:56:33 PM
hooks之useEffect
React之props传值并对其进行限制

← hooks之useEffect React之props传值并对其进行限制→

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式