最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決React報錯Property 'X' does not exist on type 'HTMLElement'

 更新時間:2022年12月05日 10:27:47   作者:Borislav Hadzhiev  
這篇文章主要為大家介紹了解決React報錯Property 'X' does not exist on type 'HTMLElement',有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

總覽

在React中,當(dāng)我們試圖訪問類型為HTMLElement 的元素上不存在的屬性時,就會發(fā)生Property 'X' does not exist on type 'HTMLElement'錯誤。為了解決該錯誤,在訪問屬性之前,使用類型斷言來正確地類型聲明元素。

這里有三個例子來展示錯誤是如何發(fā)生的。

// App.tsx
import {useEffect} from 'react';
export default function App() {
  useEffect(() => {
    const input = document.getElementById('first_name');
    // ?? Property 'value' does not exist on type 'HTMLElement'.ts(2339)
    console.log(input?.value);
    // -----------------------------------------------------------------
    const link = document.getElementById('link');
    // ?? Property 'href' does not exist on type 'HTMLElement'.ts(2339)
    console.log(link?.href);
    // -----------------------------------------------------------------
    const button = document.getElementById('btn');
    if (button != null) {
      // ?? Property 'disabled' does not exist on type 'HTMLElement'.ts(2339)
      button.disabled = true;
    }
  }, []);
  return (
    <div>
      <input
        id="first_name"
        type="text"
        name="first_name"
        defaultValue="Initial Value"
      />
      <a id="link" href="<https://google.com>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  target="_blank" rel="noreferrer">
        Open Google
      </a>
      <button id="btn">Submit</button>
    </div>
  );
}

產(chǎn)生錯誤的原因是,document.getElementById方法的返回類型是HTMLElement | null,但是我們試圖訪問的屬性不存在于HTMLElement 類型。

類型斷言

為了解決這個錯誤,使用類型斷言來為元素正確地進(jìn)行類型聲明。比如說,類型斷言為HTMLInputElementHTMLButtonElementHTMLAnchorElementHTMLImageElementHTMLDivElementHTMLTextAreaElement等等。這取決于你所處理的元素。

這些類型始終命名為HTML***Element 。一旦你開始輸入HTML…,你的IDE將會幫你自動補(bǔ)全。

import {useEffect} from 'react';
export default function App() {
  useEffect(() => {
    // ? type elements correctly via type assertions
    const input = document.getElementById('first_name') as HTMLInputElement;
    console.log(input?.value);
    const link = document.getElementById('link') as HTMLAnchorElement;
    console.log(link?.href);
    const button = document.getElementById('btn') as HTMLButtonElement;
    if (button != null) {
      button.disabled = true;
    }
  }, []);
  return (
    <div>
      <input
        id="first_name"
        type="text"
        name="first_name"
        defaultValue="Initial Value"
      />
      <a id="link" href="<https://google.com>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  target="_blank" rel="noreferrer">
        Open Google
      </a>
      <button id="btn">Submit</button>
    </div>
  );
}

類型斷言被用于我們知道值的類型信息,但是TypeScript卻不知道的時候。

我們明確的告訴TypeScript,input變量上存儲了HTMLInputElement ,并讓TS不要擔(dān)心。

同樣的,我們將link變量類型聲明為HTMLAnchorElement,將btn變量類型聲明為HTMLButtonElement 。

你可以在訪問一個屬性之前,內(nèi)聯(lián)使用類型斷言。

import {useEffect} from 'react';
export default function App() {
  useEffect(() => {
    const value = (document.getElementById('first_name') as HTMLInputElement).value;
    console.log(value);
  }, []);
  return (
    <div>
      <input
        id="first_name"
        type="text"
        name="first_name"
        defaultValue="Initial Value"
      />
      <a id="link" href="<https://google.com>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  target="_blank" rel="noreferrer">
        Open Google
      </a>
      <button id="btn">Submit</button>
    </div>
  );
}

如果你只需要訪問屬性一次,并且不希望將元素分配給變量,那么內(nèi)聯(lián)類型聲明可以完成這項(xiàng)工作。

如果你想更精確地處理元素的類型,可以使用聯(lián)合類型將類型聲明為HTML***Element | null 。

import {useEffect} from 'react';
export default function App() {
  useEffect(() => {
    const input = document.getElementById(
      'first_name',
    ) as HTMLInputElement | null;
    console.log(input?.value);
    const link = document.getElementById('link') as HTMLAnchorElement | null;
    console.log(link?.href);
    const button = document.getElementById('btn') as HTMLButtonElement | null;
    if (button != null) {
      button.disabled = true;
    }
  }, []);
  return (
    <div>
      <input
        id="first_name"
        type="text"
        name="first_name"
        defaultValue="Initial Value"
      />
      <a id="link" href="<https://google.com>" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  target="_blank" rel="noreferrer">
        Open Google
      </a>
      <button id="btn">Submit</button>
    </div>
  );
}

HTML***Element 或者null 類型是最準(zhǔn)確的類型,因?yàn)槿绻鸇OM元素上不存在id屬性,那么document.getElementById()將會返回null。

你可以使用可選鏈操作符(?.)在訪問屬性之前來進(jìn)行短路運(yùn)算,如果引用是空值(null或者undefined)的話。

或者,你可以使用簡單的if語句作為類型守衛(wèi),就像我們對button處理的那樣。

總結(jié)

最佳實(shí)踐是在類型斷言中包含null 。因?yàn)槿绻厣厦娌惶峁?code>id屬性,那么getElementById方法將會返回null。

以上就是解決React報錯Property 'X' does not exist on type 'HTMLElement'的詳細(xì)內(nèi)容,更多關(guān)于React報錯解決Property X的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • React路由跳轉(zhuǎn)的實(shí)現(xiàn)示例

    React路由跳轉(zhuǎn)的實(shí)現(xiàn)示例

    在React中,可以使用多種方法進(jìn)行路由跳轉(zhuǎn),本文主要介紹了React路由跳轉(zhuǎn)的實(shí)現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • React JSX基礎(chǔ)語法教程示例

    React JSX基礎(chǔ)語法教程示例

    這篇文章主要為大家介紹了React JSX基礎(chǔ)語法教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • React Store及store持久化的使用教程

    React Store及store持久化的使用教程

    這篇文章主要介紹了React Store及store持久化的使用教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • react-router-dom的使用說明

    react-router-dom的使用說明

    這篇文章主要介紹了react-router-dom的使用說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 重新理解?React?useRef原理

    重新理解?React?useRef原理

    這篇文章主要為大家介紹了React?useRef原理的深入理解分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • 使用React+SpringBoot開發(fā)一個協(xié)同編輯的表格文檔實(shí)現(xiàn)步驟

    使用React+SpringBoot開發(fā)一個協(xié)同編輯的表格文檔實(shí)現(xiàn)步驟

    隨著云計(jì)算和團(tuán)隊(duì)協(xié)作的興起,協(xié)同編輯成為了許多企業(yè)和組織中必不可少的需求,本文小編就將為大家介紹如何使用React+SpringBoot簡單的開發(fā)一個協(xié)同編輯的表格文檔,感興趣的朋友一起看看吧
    2023-11-11
  • React實(shí)現(xiàn)路由鑒權(quán)的實(shí)例詳解

    React實(shí)現(xiàn)路由鑒權(quán)的實(shí)例詳解

    React應(yīng)用中的路由鑒權(quán)是確保用戶僅能訪問其授權(quán)頁面的方式,用于已登錄或具有訪問特定頁面所需的權(quán)限,這篇文章就來記錄下React實(shí)現(xiàn)路由鑒權(quán)的流程,需要的朋友可以參考下
    2024-07-07
  • React Hooks之useRef獲取元素示例詳解

    React Hooks之useRef獲取元素示例詳解

    這篇文章主要介紹了React Hooks之useRef獲取元素示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • React-View-UI組件庫封裝Loading加載中源碼

    React-View-UI組件庫封裝Loading加載中源碼

    這篇文章主要介紹了React-View-UI組件庫封裝Loading加載樣式,主要包括組件介紹,組件源碼及組件測試源碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • React自定義hook的方法

    React自定義hook的方法

    Hook是 React 16.8 的新增特性。它通常與函數(shù)式組件同時使用??梢允购瘮?shù)式組件在不編寫 class 的情況下,可以擁有class組件的狀態(tài)、生命周期、引用等功能,這篇文章主要介紹了React自定義hook的相關(guān)知識,需要的朋友可以參考下
    2022-06-06

最新評論

临朐县| 长海县| 修水县| 鹤壁市| 龙陵县| 莫力| 安达市| 胶南市| 二连浩特市| 昌黎县| 浙江省| 连南| 南宫市| 隆子县| 泗水县| 南郑县| 那坡县| 奉新县| 沾益县| 收藏| 贵阳市| 凤城市| 五原县| 来凤县| 汉沽区| 原阳县| 吐鲁番市| 白朗县| 西丰县| 婺源县| 沽源县| 长汀县| 洛南县| 郧西县| 绵竹市| 沙湾县| 扎赉特旗| 安庆市| 中江县| 仁化县| 乐安县|