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

這里有個例子來展示錯誤是如何發(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;
我們嘗試將一個React組件作為屬性傳遞給Wrapper組件,但我們將該React組件的類型聲明為JSX.Element。
React.ElementType
為了解決該錯誤,將屬性的類型聲明為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可以為元素期望的屬性類型傳遞一個泛型。
在這個例子中,我們必須傳遞給它一個具有字符串類型的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)在我們顯式地聲明了元素在使用時所接受的comp屬性的類型。這有助于我們在向組件傳遞屬性時利用IDE的自動完成功能。
我們也可以使用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元素作為屬性傳遞給組件,并且不是一個真正的組件,那么使用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了一個真正的JSX元素(不是組件)到Wrapper組件上。
我們傳遞了一個JSX元素,是因?yàn)槲覀儗?code>comp={<Heading />}作為屬性進(jìn)行傳遞,而不是comp={(props) => <h2>Hello world</h2>}。
需要注意的是,在第一種情況下,我們傳遞的是一個JSX元素屬性。而在第二種情況下,我們傳遞的是一個返回JSX元素的函數(shù)(一個功能組件)。
在Wrapper組件中,我們不應(yīng)嘗試使用JSX元素作為組件。比如說,不要這么寫<Comp />,而要這么寫{Comp}。
我們沒有傳遞一個真正的組件作為屬性,我們傳遞的是一個JSX元素,所以它不應(yīng)該作為一個組件使用。
更新類型包
如果前面的建議都沒有幫助,試著通過運(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報錯JSX element type does not have any construct or call signatures的詳細(xì)內(nèi)容,更多關(guān)于React JSX element報錯解決的資料請關(guān)注腳本之家其它相關(guān)文章!
- 解決React報錯Style prop value must be an object
- 解決React報錯The?tag?is?unrecognized?in?this?browser
- 解決React報錯Cannot assign to 'current' because it is a read-only property
- 解決React報錯Functions are not valid as a React child
- 解決React報錯Encountered?two?children?with?the?same?key
- 解決React報錯Expected?`onClick`?listener?to?be?a?function
- 解決React報錯Unexpected default export of anonymous function
- 解決React報錯useNavigate()?may?be?used?only?in?context?of?Router
相關(guān)文章
react-three-fiber實(shí)現(xiàn)炫酷3D粒子效果首頁
這篇文章主要介紹了react-three-fiber實(shí)現(xiàn)3D粒子效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
React Hook - 自定義Hook的基本使用和案例講解
自定義Hook本質(zhì)上只是一種函數(shù)代碼邏輯的抽取,嚴(yán)格意義上來說,它本身并不算React的特性,這篇文章主要介紹了React類組件和函數(shù)組件對比-Hooks的介紹及初體驗(yàn),需要的朋友可以參考下2022-11-11
淺談React + Webpack 構(gòu)建打包優(yōu)化
本篇文章主要介紹了淺談React + Webpack 構(gòu)建打包優(yōu)化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
react源碼層深入刨析babel解析jsx實(shí)現(xiàn)
同作為MVVM框架,React相比于Vue來講,上手更需要JavaScript功底深厚一些,本系列將閱讀React相關(guān)源碼,從jsx -> VDom -> RDOM等一些列的過程,將會在本系列中一一講解2022-10-10
react如何使用useRef模仿抖音標(biāo)題里面添加標(biāo)簽內(nèi)容
本文介紹了如何模仿抖音發(fā)布頁面,使用div元素作為輸入框,并利用CSS樣式和JavaScript動態(tài)操作DOM,實(shí)現(xiàn)類似于input輸入框的功能,感興趣的朋友跟隨小編一起看看吧2024-10-10
React函數(shù)組件useContext useReducer自定義hooks
這篇文章主要為大家介紹了React函數(shù)組件useContext useReducer自定義hooks示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

