網(wǎng)站首頁 編程語言 正文
摘要
不管在Vue中還是React,如果我們想使用一個(gè)元素的DOM,不需要通過JS中操縱DOM的方法,它們提供了一個(gè)專屬的API就是ref。
而Vue中的ref可能比較簡(jiǎn)單,這一篇主要講一下如何在React中使用ref,以及使用ref的場(chǎng)景。
1.ref的掛載
在React中,ref可以掛載到html元素上,同時(shí)也可以掛載在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>
)
}
}
控制臺(tái)的打印為:
可以看到,在React中,ref是可以掛載到HTML元素和React元素上的。
(1)掛載HTML元素,返回真實(shí)的DOM
(2)掛載React元素,返回render后的實(shí)例對(duì)象
同時(shí)React也提供了一個(gè)方法findDOMNode可以將React元素的ref返回變成真實(shí)的DOM元素。
import { findDOMNode } from 'react-dom'
console.log(findDOMNode(this.refs.child));
同時(shí)在上面的代碼我們也可以看出來,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ù)可能還會(huì)廢棄。
(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,獲取真實(shí)的DOM元素。
3.ref的使用場(chǎng)景
這里我們說一個(gè)比較常見的場(chǎng)景,就是點(diǎn)擊按鈕讓輸入框聚焦:
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框進(jìn)行聚焦。
同時(shí)ref也可以適用于一些DOM元素的動(dòng)畫效果,例如移動(dòng),變大變小,都需要通過ref來控制DOM,進(jìn)行操作。
原文鏈接:https://blog.csdn.net/weixin_46726346/article/details/127545244
相關(guān)推薦
- 2022-03-31 SQL?Server的觸發(fā)器詳解_MsSql
- 2022-04-28 深入淺出理解C語言指針的綜合應(yīng)用_C 語言
- 2023-11-20 帶寬單位是什么?帶寬單位詳解?帶寬單位如何換算?
- 2022-04-09 python中異常的傳播詳解_python
- 2022-02-11 g2繪制點(diǎn)圖,鼠標(biāo)移入點(diǎn)上出現(xiàn)tooltip,對(duì)應(yīng)的label消失
- 2022-05-10 在 VSCode 中如何設(shè)置默認(rèn)的瀏覽器為Chrome或Firefox
- 2022-09-25 Linux中安裝和配置Redis
- 2022-05-08 Python?sns.distplot()方法的使用方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支