欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

解決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將會幫你自動補全。

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)類型聲明可以完成這項工作。

如果你想更精確地處理元素的類型,可以使用聯(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)確的類型,因為如果DOM元素上不存在id屬性,那么document.getElementById()將會返回null。

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

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

總結(jié)

最佳實踐是在類型斷言中包含null 。因為如果元素上面不提供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+umi+typeScript創(chuàng)建項目的過程

    React+umi+typeScript創(chuàng)建項目的過程

    這篇文章主要介紹了React+umi+typeScript創(chuàng)建項目的過程,結(jié)合代碼介紹了項目框架搭建的方式,本文給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • 深入理解React高階組件

    深入理解React高階組件

    本篇文章主要介紹了深入理解React高階組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 使用React-Router實現(xiàn)前端路由鑒權(quán)的示例代碼

    使用React-Router實現(xiàn)前端路由鑒權(quán)的示例代碼

    這篇文章主要介紹了使用React-Router實現(xiàn)前端路由鑒權(quán)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 詳解react-refetch的使用小例子

    詳解react-refetch的使用小例子

    這篇文章主要介紹了詳解react-refetch的使用小例子,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • React使用useEffect解決setState副作用詳解

    React使用useEffect解決setState副作用詳解

    這篇文章主要為大家介紹了React使用useEffect解決setState副作用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • react中props 的使用及進(jìn)行限制的方法

    react中props 的使用及進(jìn)行限制的方法

    這篇文章主要介紹了react中的props 的使用及進(jìn)行限制的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-04-04
  • antd4里table滾動的實現(xiàn)

    antd4里table滾動的實現(xiàn)

    本文主要介紹了antd4里table滾動的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • react組件memo useMemo useCallback使用區(qū)別示例

    react組件memo useMemo useCallback使用區(qū)別示例

    這篇文章主要為大家介紹了react組件memo useMemo useCallback使用區(qū)別的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • 詳解React??App.js?文件的結(jié)構(gòu)和作用

    詳解React??App.js?文件的結(jié)構(gòu)和作用

    在React應(yīng)用中,App.js文件通常是項目的根組件文件,它負(fù)責(zé)組織和渲染其他組件,是應(yīng)用的核心部分,本文將詳細(xì)介紹App.js文件的結(jié)構(gòu)、作用和最佳實踐,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • React之使用useState異步刷新的問題

    React之使用useState異步刷新的問題

    這篇文章主要介紹了React之使用useState異步刷新的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論