最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決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)我們試圖將元素或react組件作為屬性傳遞給另一個(gè)組件,但是屬性的類型聲明錯(cuò)誤時(shí),會(huì)產(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)文章!

相關(guān)文章

最新評論

二连浩特市| 谢通门县| 电白县| 鱼台县| 梁河县| 平顶山市| 云安县| 东丰县| 泰宁县| 万山特区| 敖汉旗| 徐水县| 启东市| 洪泽县| 池州市| 定远县| 临颍县| 咸丰县| 巴塘县| 石河子市| 扎鲁特旗| 宣武区| 射阳县| 吴桥县| 肃北| 家居| 长顺县| 昌图县| 吴堡县| 谷城县| 保定市| 屏东市| 玉门市| 庄浪县| 济源市| 铁岭县| 西充县| 上栗县| 阿拉善盟| 旬阳县| 奉新县|