網(wǎng)站首頁 編程語言 正文
摘要
不管在Vue中還是React,如果我們想使用一個元素的DOM,不需要通過JS中操縱DOM的方法,它們提供了一個專屬的API就是ref。
而Vue中的ref可能比較簡單,這一篇主要講一下如何在React中使用ref,以及使用ref的場景。
1.ref的掛載
在React中,ref可以掛載到html元素上,同時也可以掛載在React元素上,看下面的代碼:
import React, { Component } from 'react'
// import { findDOMNode } from 'react-dom'
import Child from './Child'
export default class Father extends Component {
componentDidMount(){
console.log(this.refs.refElement);
console.log(this.refs.child);
}
render() {
return (
<div>
<input ref={ 'refElement' }></input>
<Child ref={ 'child' }/>
<button onClick={this.fn}>123</button>
</div>
)
}
}
控制臺的打印為:
可以看到,在React中,ref是可以掛載到HTML元素和React元素上的。
(1)掛載HTML元素,返回真實的DOM
(2)掛載React元素,返回render后的實例對象
同時React也提供了一個方法findDOMNode可以將React元素的ref返回變成真實的DOM元素。
import { findDOMNode } from 'react-dom'
console.log(findDOMNode(this.refs.child));
同時在上面的代碼我們也可以看出來,ref的掛載是在componentDidMount等生命周期之前執(zhí)行的。
2.使用ref的三種方式
(1)字符串的方式
import React, { Component } from 'react'
export default class Father extends Component {
componentDidMount(){
console.log(this.refs.refElement);
}
render() {
return (
<div>
<input ref={ 'refElement' }></input>
<button onClick={this.fn}>123</button>
</div>
)
}
}
這種方式和Vue的ref比較相似,但是官方目前已經(jīng)不推薦使用該方式,后續(xù)可能還會廢棄。
(2)函數(shù)的方式
import React, { Component } from 'react'
export default class Father extends Component {
componentDidMount(){
console.log(this.refElement);
}
render() {
return (
<div>
<input ref={ ref => this.refElement = ref }></input>
<button onClick={this.fn}>123</button>
</div>
)
}
}
(3)react.CreateRef的方式
import React, { Component } from 'react'
export default class Father extends Component {
refElement = React.createRef();
componentDidMount(){
console.log(this.refElement.current);
}
render() {
return (
<div>
<input ref={this.refElement}></input>
<button onClick={this.fn}>123</button>
</div>
)
}
}
記住這里面通過refElement中的current,獲取真實的DOM元素。
3.ref的使用場景
這里我們說一個比較常見的場景,就是點擊按鈕讓輸入框聚焦:
import React, { Component } from 'react'
export default class Father extends Component {
refElement = React.createRef();
componentDidMount(){
console.log(this.refElement.current);
}
fn = ()=>{
this.refElement.current.focus();
}
render() {
return (
<div>
<input ref={this.refElement}></input>
<button onClick={this.fn}>聚焦</button>
</div>
)
}
}
通過獲取DOM后,調(diào)用DOM上的focus方法API,來讓input框進行聚焦。
同時ref也可以適用于一些DOM元素的動畫效果,例如移動,變大變小,都需要通過ref來控制DOM,進行操作。
原文鏈接:https://blog.csdn.net/weixin_46726346/article/details/127545244
相關(guān)推薦
- 2022-06-30 python神經(jīng)網(wǎng)絡tensorflow利用訓練好的模型進行預測_python
- 2022-10-11 slearn缺失值處理器之Imputer詳析_python
- 2022-11-16 C語言數(shù)據(jù)結(jié)構(gòu)不掛科指南之線性表詳解_C 語言
- 2022-10-23 C#位運算符的基本用法介紹_C#教程
- 2023-07-14 react 中redux的使用步驟
- 2023-01-08 基于C#實現(xiàn)屏幕取色器的示例詳解_C#教程
- 2023-06-18 C#中如何連接海康威視_C#教程
- 2022-07-01 python神經(jīng)網(wǎng)絡ShuffleNetV2模型復現(xiàn)詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支