網站首頁 編程語言 正文
解決React報錯Property?value?does?not?exist?on?type?HTMLElement_React
作者:Borislav?Hadzhiev ? 更新時間: 2022-12-29 編程語言總覽
當我們試圖訪問一個類型為HTMLElement
的元素上的value
屬性時,會產生"Property 'value' does not exist on type 'HTMLElement'"錯誤。為了解決該錯誤,在訪問屬性之前,使用類型斷言將元素類型斷言為HTMLInputElement
。
這里有個示例用來展示錯誤是如何發生的。
// App.tsx import {useEffect} from 'react'; export default function App() { useEffect(() => { const input = document.getElementById('message'); // ?? Property 'value' does not exist on type 'HTMLElement'.ts(2339) console.log(input?.value); }, []); return ( <div> <input id="message" defaultValue="Initial value" /> </div> ); }
我們得到錯誤的原因是因為,document.getElementById方法返回的類型為HTMLElement | null
,并且value屬性不存在于HTMLElement
類型上。
類型斷言
為了解決該錯誤,使用類型斷言將元素類型斷言為HTMLInputElement
(或者HTMLTextAreaElement
,如果你使用textarea
元素鍵入)。
import {useEffect} from 'react'; export default function App() { useEffect(() => { // ? type element as HTMLInputElement const input = document.getElementById('message') as HTMLInputElement; console.log(input?.value); // ?? "Initial value" }, []); return ( <div> <input id="message" defaultValue="Initial value" /> </div> ); }
你也可以在內聯中使用一個類型斷言,就在訪問值屬性之前。
// App.tsx import {useEffect} from 'react'; export default function App() { useEffect(() => { // ?? inline type assertion const value = (document.getElementById('message') as HTMLInputElement).value; console.log(value); }, []); return ( <div> <input id="message" defaultValue="Initial value" /> </div> ); }
當我們擁有一個值的類型信息,但是TypeScript無從得知時,就會使用類型斷言。
我們有效地告訴TypeScript,input
變量存儲了一個HTMLInputElement
,不用擔心它。
如果你正在使用一個textarea
元素,你將使用HTMLTextAreaElement
類型來代替。
聯合類型
如果你想更精確地控制類型,你可以使用一個聯合類型來設置類型為HTMLInputElement | null
。
// App.tsx import {useEffect} from 'react'; export default function App() { useEffect(() => { // ? type element as HTMLInputElement | null const input = document.getElementById('message') as HTMLInputElement | null; console.log(input?.value); // ?? "Initial value" }, []); return ( <div> <input id="message" defaultValue="Initial value" /> </div> ); }
HTMLInputElement | null
類型是正確的,因為如果提供id的元素不存在于DOM中,document.getElementById()
方法就會返回一個null
值。
需要注意的是,我們使用了可選鏈(?.
)操作符來短路運算,如果引用是空值的話(null
或者undefined
)。
換句話說,如果input
變量存儲了一個null
值,我們就不會試圖訪問null
的屬性,而得到一個運行時錯誤。
類型守衛
你也可以使用一個簡單的if
語句作為類型守衛,以確保input
變量不存儲一個null
值。
// App.tsx import {useEffect} from 'react'; export default function App() { useEffect(() => { const input = document.getElementById('message') as HTMLInputElement | null; if (input != null) { console.log(input.value); // ?? "Initial value" } }, []); return ( <div> <input id="message" defaultValue="Initial value" /> </div> ); }
TypeScript知道input
變量在if
塊中的類型是HTMLInputElement
,并允許我們直接訪問其value
屬性。
在類型斷言中包含null
總是一種最佳實踐,因為如果沒有找到所提供的id
的元素,getElementById
方法將返回null
。
原文鏈接:bobbyhadz.com/blog/react-…
原文鏈接:https://juejin.cn/post/7136173375247876110
相關推薦
- 2022-06-15 C++詳細講解繼承與虛繼承實現_C 語言
- 2022-04-01 k8s 學習 kubeadm join 超時報錯 : error uploading crisock
- 2022-04-03 Python中的tkinter庫簡單案例詳解_python
- 2022-06-22 SQL實現篩選出連續3天登錄用戶與窗口函數的示例代碼_MsSql
- 2022-02-28 純 CSS實現根據元素已知的寬度設置高度以及注意事項
- 2022-06-06 Array.prototype.myjoin
- 2023-04-22 python的open函數使用案例代碼_python
- 2022-11-13 Flutter入門學習Dart語言變量及基本使用概念_Dart
- 最近更新
-
- 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同步修改后的遠程分支