【发布时间】:2020-05-16 05:23:30
【问题描述】:
我正在学习 TypeScript,有些地方让我感到困惑。下面是一点:
interface Props {
name: string;
}
const PrintName: React.FC<Props> = (props) => {
return (
<div>
<p style={{ fontWeight: props.priority ? "bold" : "normal" }}>
{props.name}
</p>
</div>
)
}
const PrintName2 = (props: Props) => {
return (
<div>
<p style={{ fontWeight: props.priority ? "bold" : "normal" }}>
{props.name}
</p>
</div>
)
}
对于上面的两个功能组件,我看到 TypeScript 生成了相同的 JS 代码。就可读性而言,PrintName2 组件对我来说似乎更加精简。我想知道这两种定义有什么区别,是否有人在使用第二种类型的 React 组件?
【问题讨论】:
-
在这里回答 stackoverflow.com/questions/44133420/… 更多关于你可以在 React.FC 中找到的属性。
标签: javascript reactjs typescript react-functional-component