簡體   English   中英

使用打字稿將自定義道具傳遞給反應組件

[英]Passing custom props to a react component using typescript

我對 TypeScript 很陌生,我想知道是否有人可以給我一個這個 React 組件轉換為 TypeScript 的例子:

const Message = ({from, text}) => (
  <div>
    <p>{text}</p>
    <p>from: {from}</p>
  </div>  
);

我試過這樣的事情:

const Message: React.FC = ({from, text}) => (
  <div>
    <p>{text}</p>
    <p>from: {from}</p>
  </div>  
);

但它給了我錯誤:類型 '{children?: ReactNode; 上不存在屬性 'propertyName'。 }'。

interface Props {
  from: string;
  to: string;
}

const Message: React.FC<Props> = ({from, text}) => (
  <div>
    <p>{text}</p>
    <p>from: {from}</p>
  </div>  
);

你只需要讓 Typescript 知道組件的 props 是什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM