網站首頁 編程語言 正文
React獲取DOM的方式
ref獲取DOM元素
在React的開發模式中,通常情況下不需要、也不建議直接操作DOM原生,但是某些特殊的情況,確實需要獲取到DOM進行某些操作:
管理焦點,文本選擇或媒體播放;
觸發強制動畫;
集成第三方 DOM 庫;
我們可以通過refs獲取DOM;
如何創建refs來獲取對應的DOM呢?目前有三種方式:
方式一:傳入字符串
(這種做法已經不推薦)
在React元素上綁定一個ref字符串, 使用時通過
this.refs.傳入的字符串
格式獲取對應的元素;
import React, { PureComponent } from 'react' export class App extends PureComponent { getDom() { // 方式一 console.log(this.refs.hello) // <h2>Hello World</h2> } render() { return ( <div> <h2 ref="hello">Hello World</h2> <button onClick={() => this.getDom()}>獲取DOM</button> </div> ) } } export default App
方式二:傳入一個對象
(常用的方式, 推薦)
在react中導入createRef, 通過
createRef()
方式提前創建出來一個對象, 將創建出來的對象綁定到要獲取的元素上;使用時獲取到創建的對象其中有一個
current
屬性就是對應的元素;
import React, { PureComponent, createRef } from 'react' export class App extends PureComponent { constructor() { super() // 提前創建一個ref對象 this.titleRef = createRef() } getDom() { // 方式二 console.log(this.titleRef.current) // <h2>Hello World</h2> } render() { return ( <div> <h2 ref={this.titleRef}>Hello World</h2> <button onClick={() => this.getDom()}>獲取DOM</button> </div> ) } } export default App
方式三:傳入一個函數
(了解)
該函數會在DOM被掛載時進行回調,這個函數回調時會傳入一個元素對象,我們可以自己保存;
使用時,直接拿到之前保存的元素對象即可;
import React, { PureComponent, createRef } from 'react' export class App extends PureComponent { getDom() { } render() { return ( <div> <h2 ref="hello">Hello World</h2> <h2 ref={this.titleRef}>Hello World</h2> {/* 方式三, 回調函數會返回el, el就是我們要獲取的DOM了 */} <h2 ref={el => console.log(el)}>Hello World</h2> <button onClick={() => this.getDom()}>獲取DOM</button> </div> ) } }
ref獲取組件實例
ref 的值根據節點的類型而有所不同:
當 ref 屬性用于 HTML 元素時,構造函數中使用 React.createRef() 創建的 ref 接收底層 DOM 元素作為其 current 屬性;
當 ref 屬性用于自定義 class 組件時,ref 對象接收組件的掛載實例作為其 current 屬性;
你
不能在函數組件上使用 ref 屬性
,因為他們沒有實例;
這里我們演示一下ref獲取一個class組件對象的實例:
import React, { PureComponent, createRef } from 'react' // 創建一個類組件, 作為子組件測試 class Test extends PureComponent { test() { console.log("Test"); } render() { return <h2>Test</h2> } } export class App extends PureComponent { constructor() { super() this.tsetRef = createRef() } getDom() { // 獲取組件實例 console.log(this.tsetRef.current) // 可以調用組件的實例方法 this.tsetRef.current.test() } render() { return ( <div> <Test ref={this.tsetRef}/> </div> ) } } export default App
函數式組件是沒有實例的,所以無法通過ref獲取他們的實例:
但是某些時候,我們可能想要獲取函數式組件中的某個DOM元素;
這個時候我們可以通過
React.forwardRef
來綁定函數組件中的某個元素, forwardRef中接收兩個參數, 參數一: props, 參數二: ref,后面我們也會學習 hooks 中如何使用ref;
import { render } from '@testing-library/react'; import React, { PureComponent, createRef, forwardRef } from 'react' } // 創建一個函數組件, 作為子組件測試 // 使用forwardRef將函數包裹起來 const Foo = forwardRef(function(props, ref) { return ( <h2 ref={ref}>Foo</h2> ) }) export class App extends PureComponent { constructor() { super() // 提前創建一個ref對象 this.fooRef = createRef() } getDom() { // 獲取函數組件中元素的DOM console.log(this.fooRef.current) } render() { return ( <div> <Foo ref={this.fooRef}/> </div> ) } } export default App
原文鏈接:https://blog.csdn.net/m0_71485750/article/details/126658749
相關推薦
- 2022-10-13 C++智能指針詳解_C 語言
- 2022-09-28 Python使用captcha制作驗證碼的實現示例_python
- 2023-04-24 如何修改Linux內核參數vm.swappiness_Linux
- 2022-11-09 PostgreSQL索引掃描時為什么index?only?scan不返回ctid_PostgreSQ
- 2023-12-18 YAMLException異常產生原因及解決方案
- 2023-01-13 Matlab中的mat數據轉成python中使用的npy數據遇到的坑及解決_python
- 2023-12-09 springframework.jdbc.BadSqlGrammarException:
- 2022-07-17 Python?OpenGL基本配置方式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支