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

這里有個(gè)例子來展示錯(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;
請注意,React.ElementType可以為元素期望的屬性類型傳遞一個(gè)泛型。
在這個(gè)例子中,我們必須傳遞給它一個(gè)具有字符串類型的name屬性的對象,因?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屬性的類型。這有助于我們在向組件傳遞屬性時(shí)利用IDE的自動(dòng)完成功能。
我們也可以使用React.ComponentType,但這樣我們就需要對屬性聲明類型。
// 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元素作為組件。比如說,不要這么寫<Comp />,而要這么寫{Comp}。
我們沒有傳遞一個(gè)真正的組件作為屬性,我們傳遞的是一個(gè)JSX元素,所以它不應(yīng)該作為一個(gè)組件使用。
更新類型包
如果前面的建議都沒有幫助,試著通過運(yùn)行以下命令來更新你的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ò)解決的資料請關(guān)注腳本之家其它相關(guān)文章!
- 解決React報(bào)錯(cuò)Style prop value must be an object
- 解決React報(bào)錯(cuò)The?tag?is?unrecognized?in?this?browser
- 解決React報(bào)錯(cuò)Cannot assign to 'current' because it is a read-only property
- 解決React報(bào)錯(cuò)Functions are not valid as a React child
- 解決React報(bào)錯(cuò)Encountered?two?children?with?the?same?key
- 解決React報(bào)錯(cuò)Expected?`onClick`?listener?to?be?a?function
- 解決React報(bào)錯(cuò)Unexpected default export of anonymous function
- 解決React報(bào)錯(cuò)useNavigate()?may?be?used?only?in?context?of?Router
相關(guān)文章
用React實(shí)現(xiàn)一個(gè)類 chatGPT 的交互式問答組件的方法詳解
這篇文章主要給大家詳細(xì)介紹如何用React實(shí)現(xiàn)一個(gè)類 chatGPT 的交互式問答組件的方法,文中有詳細(xì)的代碼示例,對我們學(xué)習(xí)有一定的幫助,需要的朋友可以參考下2023-06-06
React?Router?v6路由懶加載的2種方式小結(jié)
React?Router?v6?的路由懶加載有2種實(shí)現(xiàn)方式,1是使用react-router自帶的?route.lazy,2是使用React自帶的?React.lazy,下面我們就來看看它們的具體實(shí)現(xiàn)方法吧2024-04-04
React路由渲染方式與withRouter高階組件及自定義導(dǎo)航組件應(yīng)用詳細(xì)介紹
這篇文章主要介紹了React路由三種渲染方式、withRouter高階組件、自定義導(dǎo)航組件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
詳解React+Koa實(shí)現(xiàn)服務(wù)端渲染(SSR)
這篇文章主要介紹了詳解React+Koa實(shí)現(xiàn)服務(wù)端渲染(SSR),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
React項(xiàng)目中動(dòng)態(tài)插入HTML內(nèi)容的實(shí)現(xiàn)
本文主要介紹了React項(xiàng)目中動(dòng)態(tài)插入HTML內(nèi)容的實(shí)現(xiàn),通過使用React的dangerouslySetInnerHTML屬性,我們可以將HTML內(nèi)容插入到組件中,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
React Ant Design樹形表格的復(fù)雜增刪改操作
這篇文章主要介紹了React Ant Design樹形表格的復(fù)雜增刪改操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Hello?React的組件化方式之React入門小案例演示
這篇文章主要介紹了Hello?React的組件化方式-React入門小案例,本文通過Hello?React的案例,?來體驗(yàn)一下React開發(fā)模式,?以及jsx的語法,需要的朋友可以參考下2022-10-10

