reactjs - 使用打字稿将自定义道具传递给反应组件
问题描述
我对 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>
);
但它给了我错误:类型'{孩子?: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 是什么。
推荐阅读
- java - 无法在 HttpServletRequest 中获取证书 - httpServletRequest.getAttribute("javax.servlet.request.X509Certificate") 给出 null
- raku - 如何死于未定义的值?
- javascript - 输入特定字符时,在克林特写入输入字段期间更改文本颜色
- scikit-learn - 在 PLS 模型中查找每个原始描述符的整体贡献
- dataweave - 如何将数据映射到 Dataweave 中的数组?
- javascript - 创建可折叠菜单和子菜单时如何修复间距
- android - 是否可以用房间实现实时分页(aac 库)?
- java - 向异常添加数据
- python - 如何停止计时器 discord.py
- selenium - Selenium InternetExplorerDriver 代理 C#