解決React報(bào)錯(cuò)JSX?element?type?does?not?have?any?construct?or?call?signatures
總覽
當(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)文章!
- 解決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-three-fiber實(shí)現(xiàn)炫酷3D粒子效果首頁(yè)
這篇文章主要介紹了react-three-fiber實(shí)現(xiàn)3D粒子效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08React Hook - 自定義Hook的基本使用和案例講解
自定義Hook本質(zhì)上只是一種函數(shù)代碼邏輯的抽取,嚴(yán)格意義上來(lái)說(shuō),它本身并不算React的特性,這篇文章主要介紹了React類組件和函數(shù)組件對(duì)比-Hooks的介紹及初體驗(yàn),需要的朋友可以參考下2022-11-11react實(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)化,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01react源碼層深入刨析babel解析jsx實(shí)現(xiàn)
同作為MVVM框架,React相比于Vue來(lái)講,上手更需要JavaScript功底深厚一些,本系列將閱讀React相關(guān)源碼,從jsx -> VDom -> RDOM等一些列的過(guò)程,將會(huì)在本系列中一一講解2022-10-10react如何使用useRef模仿抖音標(biāo)題里面添加標(biāo)簽內(nèi)容
本文介紹了如何模仿抖音發(fā)布頁(yè)面,使用div元素作為輸入框,并利用CSS樣式和JavaScript動(dòng)態(tài)操作DOM,實(shí)現(xiàn)類似于input輸入框的功能,感興趣的朋友跟隨小編一起看看吧2024-10-10React函數(shù)組件useContext useReducer自定義hooks
這篇文章主要為大家介紹了React函數(shù)組件useContext useReducer自定義hooks示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08