亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決React報(bào)錯(cuò)JSX?element?type?does?not?have?any?construct?or?call?signatures

 更新時(shí)間:2022年12月02日 10:52:18   作者:Borislav?Hadzhiev  
這篇文章主要為大家介紹了解決React報(bào)錯(cuò)JSX?element?type?does?not?have?any?construct?or?call?signatures,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

總覽

當(dāng)我們?cè)噲D將元素或react組件作為屬性傳遞給另一個(gè)組件,但是屬性的類型聲明錯(cuò)誤時(shí),會(huì)產(chǎn)生"JSX element type does not have any construct or call signatures"錯(cuò)誤。為了解決該錯(cuò)誤,可以使用React.ElementType類型。

這里有個(gè)例子來(lái)展示錯(cuò)誤是如何發(fā)生的。

// App.tsx
import React from 'react';
interface Props {
  comp: JSX.Element;
}
const Wrapper: React.FunctionComponent<Props> = props => {
  const {comp: Comp} = props;
  // ?? JSX element type 'Comp' does not have any construct or call signatures.ts(2604)
  return (
    <div>
      <Comp name="James" />
    </div>
  );
};
const App: React.FunctionComponent = () => {
  const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
  return (
    <div>
      <Wrapper comp={heading} />
    </div>
  );
};
export default App;

我們嘗試將一個(gè)React組件作為屬性傳遞給Wrapper組件,但我們將該React組件的類型聲明為JSX.Element

React.ElementType

為了解決該錯(cuò)誤,將屬性的類型聲明為React.ElementType

// App.tsx
import React from 'react';
interface Props {
  comp: React.ElementType; // ??? type it as React.ElementType
}
const Wrapper: React.FunctionComponent<Props> = props => {
  // ??? component names must start with capital letter
  const {comp: Comp} = props;
  return (
    <div>
      <Comp name="James" />
    </div>
  );
};
const App: React.FunctionComponent = () => {
  // ??? takes a name prop
  const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
  return (
    <div>
      <Wrapper comp={heading} />
    </div>
  );
};
export default App;

請(qǐng)注意,React.ElementType可以為元素期望的屬性類型傳遞一個(gè)泛型。

在這個(gè)例子中,我們必須傳遞給它一個(gè)具有字符串類型的name屬性的對(duì)象,因?yàn)槟鞘?code>heading組件接收的屬性。

// App.tsx
import React from 'react';
interface Props {
  // ? explicitly type props comp takes
  comp: React.ElementType<{name: string}>;
}
const Wrapper: React.FunctionComponent<Props> = props => {
  // ??? component names must start with capital letter
  const {comp: Comp} = props;
  return (
    <div>
      <Comp name="James" />
    </div>
  );
};
const App: React.FunctionComponent = () => {
  const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
  return (
    <div>
      <Wrapper comp={heading} />
    </div>
  );
};
export default App;

現(xiàn)在我們顯式地聲明了元素在使用時(shí)所接受的comp屬性的類型。這有助于我們?cè)谙蚪M件傳遞屬性時(shí)利用IDE的自動(dòng)完成功能。

我們也可以使用React.ComponentType,但這樣我們就需要對(duì)屬性聲明類型。

// App.tsx
import React from 'react';
interface Props {
  // ??? now using React.ComponentType ???
  comp: React.ComponentType<{name: string}>;
}
const Wrapper: React.FunctionComponent<Props> = props => {
  // ??? component names must start with capital letter
  const {comp: Comp} = props;
  return (
    <div>
      <Comp name="James" />
    </div>
  );
};
const App: React.FunctionComponent = () => {
  const heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
  return (
    <div>
      <Wrapper comp={heading} />
    </div>
  );
};
export default App;

React.ComponentType 中的泛型不能默認(rèn)為any類型,因此我們需要顯示地聲明屬性的類型。

傳遞JSX元素

如果你需要將JSX元素作為屬性傳遞給組件,并且不是一個(gè)真正的組件,那么使用JSX.Element類型就是正確的。

// App.tsx
import React from 'react';
interface Props {
  // ??? using JSX.Element type
  comp: JSX.Element;
}
const Wrapper: React.FunctionComponent<Props> = props => {
  const {comp: Comp} = props;
  // ??? use as {Comp}
  return <div>{Comp}</div>;
};
const App: React.FunctionComponent = () => {
  const Heading = ({name}: {name: string}) => <h2>Hello {name}</h2>;
  // ??? we are passing an actual JSX element
  // because we didn't pass it as comp={Heading}
  return (
    <div>
      <Wrapper comp={<Heading name="James" />} />
    </div>
  );
};
export default App;

我們將comp屬性的類型聲明為JSX.Element,因?yàn)槲覀儌鬟f了一個(gè)真正的JSX元素(不是組件)到Wrapper組件上。

我們傳遞了一個(gè)JSX元素,是因?yàn)槲覀儗?code>comp={<Heading />}作為屬性進(jìn)行傳遞,而不是comp={(props) => <h2>Hello world</h2>}。

需要注意的是,在第一種情況下,我們傳遞的是一個(gè)JSX元素屬性。而在第二種情況下,我們傳遞的是一個(gè)返回JSX元素的函數(shù)(一個(gè)功能組件)。

在Wrapper組件中,我們不應(yīng)嘗試使用JSX元素作為組件。比如說(shuō),不要這么寫<Comp />,而要這么寫{Comp}。

我們沒(méi)有傳遞一個(gè)真正的組件作為屬性,我們傳遞的是一個(gè)JSX元素,所以它不應(yīng)該作為一個(gè)組件使用。

更新類型包

如果前面的建議都沒(méi)有幫助,試著通過(guò)運(yùn)行以下命令來(lái)更新你的React類型的版本。

# ??? with NPM
npm install react@latest react-dom@latest
npm install --save-dev @types/react@latest @types/react-dom@latest
# ----------------------------------------------
# ??? with YARN
yarn add react@latest react-dom@latest
yarn add @types/react@latest @types/react-dom@latest --dev

原文鏈接:bobbyhadz.com/blog/react-…

以上就是解決React報(bào)錯(cuò)JSX element type does not have any construct or call signatures的詳細(xì)內(nèi)容,更多關(guān)于React JSX element報(bào)錯(cuò)解決的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • react-three-fiber實(shí)現(xiàn)炫酷3D粒子效果首頁(yè)

    react-three-fiber實(shí)現(xiàn)炫酷3D粒子效果首頁(yè)

    這篇文章主要介紹了react-three-fiber實(shí)現(xiàn)3D粒子效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08
  • React+Ts實(shí)現(xiàn)二次封裝組件

    React+Ts實(shí)現(xiàn)二次封裝組件

    本文主要介紹了React+Ts實(shí)現(xiàn)二次封裝組件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • React Hook - 自定義Hook的基本使用和案例講解

    React Hook - 自定義Hook的基本使用和案例講解

    自定義Hook本質(zhì)上只是一種函數(shù)代碼邏輯的抽取,嚴(yán)格意義上來(lái)說(shuō),它本身并不算React的特性,這篇文章主要介紹了React類組件和函數(shù)組件對(duì)比-Hooks的介紹及初體驗(yàn),需要的朋友可以參考下
    2022-11-11
  • React如何以Hook的方式使用Echarts

    React如何以Hook的方式使用Echarts

    這篇文章主要介紹了React如何以Hook的方式使用Echarts問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • react實(shí)現(xiàn)動(dòng)態(tài)選擇框

    react實(shí)現(xiàn)動(dòng)態(tài)選擇框

    這篇文章主要為大家詳細(xì)介紹了react實(shí)現(xiàn)動(dòng)態(tài)選擇框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 淺談React + Webpack 構(gòu)建打包優(yōu)化

    淺談React + Webpack 構(gòu)建打包優(yōu)化

    本篇文章主要介紹了淺談React + Webpack 構(gòu)建打包優(yōu)化,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • react源碼層深入刨析babel解析jsx實(shí)現(xiàn)

    react源碼層深入刨析babel解析jsx實(shí)現(xiàn)

    同作為MVVM框架,React相比于Vue來(lái)講,上手更需要JavaScript功底深厚一些,本系列將閱讀React相關(guān)源碼,從jsx -> VDom -> RDOM等一些列的過(guò)程,將會(huì)在本系列中一一講解
    2022-10-10
  • react如何使用useRef模仿抖音標(biāo)題里面添加標(biāo)簽內(nèi)容

    react如何使用useRef模仿抖音標(biāo)題里面添加標(biāo)簽內(nèi)容

    本文介紹了如何模仿抖音發(fā)布頁(yè)面,使用div元素作為輸入框,并利用CSS樣式和JavaScript動(dòng)態(tài)操作DOM,實(shí)現(xiàn)類似于input輸入框的功能,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • 詳細(xì)分析React 表單與事件

    詳細(xì)分析React 表單與事件

    這篇文章主要介紹了React 表單與事件的相關(guān)資料,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • React函數(shù)組件useContext useReducer自定義hooks

    React函數(shù)組件useContext useReducer自定義hooks

    這篇文章主要為大家介紹了React函數(shù)組件useContext useReducer自定義hooks示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08

最新評(píng)論