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-08

React之Refs的基本使用

在 React 中,允许我们通过 refs 来访问和操作 Dom 元素或组件实例。

在类组件中使用 refs 有三种方式:

①、字符串形式的 ref:(不推荐使用,该 API 在 React 未来的版本中将会被移除)

class App extends React.Component {
	getInput = () => {
		console.log(this.refs.myInput) // 获取input的dom元素
	}
	render () {
		return (
			<div>
				<input type="text" ref="myInput" onBlur={ this.getInput } />
			</div>
		)
	}
}

export default App;
1
2
3
4
5
6
7
8
9
10
11
12
13
14

②、回调形式的 ref:

// ref回调被定义为内联函数
class App extends React.Component {
	getInput = () => {
		console.log(this.myInput) // 获取input的dom元素
	}
	render () {
		return (
			<div>
				<input type="text" ref={ el => this.myInput = el } onBlur={ this.getInput } />
			</div>
		)
	}
}

export default App;


// 或者也可以写成下面这种形式,ref回调被定义为类上的绑定方法
class App extends React.Component {
	getInputRef = (el) => {
		this.inputRef = el
	}
	getInput = () => {
		console.log(this.inputRef)  // 获取input的dom元素
	}
	render () {
		return (
			<div>
				<input type="text" ref={ this.getInputRef } onBlur={ this.getInput } />
			</div>
		)
	}
}

export default App;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

③、使用 createRef(主要用于类组件,在函数组件中通常使用 useRef)

class App extends React.Component {
	myRef = React.createRef()
	getInput = () => {
		console.log(this.myRef.current) // 获取input的dom元素
	}
	render () {
		return (
			<div>
				<input type="text" ref={ this.myRef } onBlur={ this.getInput } />
			</div>
		)
	}
}

export default App;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

在函数组件中使用 refs 主要是通过 useRef 这个 API 来实现的

import { useRef } from "react"
export default function App () {
    const myRef = useRef(null)
    function getInput () {
        console.log(myRef.current)
    }
    return (
        <div>
            <input type="text" ref={myRef} onBlur={ getInput } />
        </div>
    )
}
1
2
3
4
5
6
7
8
9
10
11
12
编辑 (opens new window)
上次更新: 7/1/2024, 4:56:33 PM
React之props传值并对其进行限制
React之生命周期钩子

← React之props传值并对其进行限制 React之生命周期钩子→

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