[英]Passing custom props to a react component using typescript
I am pretty new to TypeScript and I am wondering if someone can give me an example of this React component converted to TypeScript:我对 TypeScript 很陌生,我想知道是否有人可以给我一个这个 React 组件转换为 TypeScript 的例子:
const Message = ({from, text}) => (
<div>
<p>{text}</p>
<p>from: {from}</p>
</div>
);
I have tried somethinig like this:我试过这样的事情:
const Message: React.FC = ({from, text}) => (
<div>
<p>{text}</p>
<p>from: {from}</p>
</div>
);
but it gives me the error: Property 'propertyName' does not exist on type '{ children?: ReactNode;但它给了我错误:类型 '{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>
);
You just need to let Typescript know what the props for the component are.你只需要让 Typescript 知道组件的 props 是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.