網站首頁 編程語言 正文
總覽
當我們試圖在其對應的DOM元素被渲染之前訪問其current
屬性時,React的ref通常會返回undefined
或者null
。為了解決該問題,可以在useEffect
鉤子中訪問ref
,或者當事件觸發時再訪問ref
。
import {useRef, useEffect} from 'react'; export default function App() { const ref = useRef(); console.log(ref.current); // ??? undefined here useEffect(() => { const el2 = ref.current; console.log(el2); // ??? element here }, []); return ( <div> <div ref={ref}> <h2>Hello</h2> </div> </div> ); }
useEffect
useRef()
鉤子可以傳遞一個初始值作為參數。該鉤子返回一個可變的ref
對象,ref
對象上的current
屬性被初始化為傳遞的參數。
我們沒有為useRef
傳遞初始值,因此其current
屬性設置為undefined
。如果我們將null
傳遞給鉤子,如果立即訪問其current
屬性,將會得到null
。
需要注意的是,我們必須訪問
ref
對象上的current
屬性,以此來訪問設置了ref
屬性的div
元素。
當我們為元素傳遞ref
屬性時,比如說,<div ref={myRef} />
,React將ref
對象的.current
屬性設置為相應的DOM節點。
我們使用useEffect
鉤子,是因為我們想要確保ref
已經設置在元素上,并且元素已經渲染到DOM上。
如果我們嘗試在組件中直接訪問ref
上的current
屬性,我們會得到undefined,是因為 ref
還沒有被設置,而且 div
元素還沒有被渲染。
事件
你也可以在事件處理函數中訪問ref
的current
屬性。
import {useRef, useEffect} from 'react'; export default function App() { const ref = useRef(); console.log(ref.current); // ??? undefined here useEffect(() => { const el2 = ref.current; console.log(el2); // ??? element here }, []); const handleClick = () => { console.log(ref.current); // ??? element here }; return ( <div> <div ref={ref}> <h2>Hello</h2> </div> <button onClick={handleClick}>Click</button> </div> ); }
當用戶點擊按鈕的時候,ref
已經被設置好了,相應的元素已經被渲染到DOM中,所以我們能夠訪問它。
總結
可以在useEffect
鉤子中訪問ref
,或者當事件觸發時再訪問ref
。也就是說,要確保元素已經渲染到DOM上。
到此這篇關于React報錯解決之ref返回undefined或null的文章就介紹到這了,更多相關React ref返回undefined null內容請搜索AB教程網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持AB教程網!
附:ref的值根據節點的類型而有所不同
- 當在refHTML元素上使用該屬性時,ref在構造函數中創建的屬性將React.createRef()接收底層DOM元素作為其current屬性。
- 在ref自定義類組件上使用該屬性時,該ref對象將接收組件的已安裝實例作為其current。
您可能無法ref在函數組件上使用該屬性,因為它們沒有實例。
class CustomTextInput extends React.Component { constructor(props) { super(props); // create a ref to store the textInput DOM element this.textInput = React.createRef(); this.focusTextInput = this.focusTextInput.bind(this); } focusTextInput() { // Explicitly focus the text input using the raw DOM API // Note: we're accessing "current" to get the DOM node this.textInput.current.focus(); } render() { // tell React that we want to associate the <input> ref // with the `textInput` that we created in the constructor return ( <div> <input type="text" ref={this.textInput} /> <input type="button" value="Focus the text input" onClick={this.focusTextInput} /> </div> ); } }
current當組件安裝時,React將為該屬性分配DOM元素,并null在卸載時將其分配回。ref更新發生之前componentDidMount或componentDidUpdate生命周期方法。
原文鏈接:bobbyhadz.com/blog/react-…
原文鏈接:https://juejin.cn/post/7126902174801625101
相關推薦
- 2022-12-22 C++?Boost?Foreach超詳細分析講解_C 語言
- 2022-11-17 python數學模塊(math/decimal模塊)_python
- 2022-10-22 Python基礎Lists和tuple實例詳解_python
- 2022-11-13 Redis中HyperLogLog的使用詳情_Redis
- 2022-11-23 Python數據清洗&預處理入門教程_python
- 2022-09-05 C語言深入淺出分析函數指針_C 語言
- 2022-09-21 Oracle數據庫對象的使用詳解_oracle
- 2022-11-17 C++11中異常處理機制詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支