【发布时间】:2020-07-15 11:34:49
【问题描述】:
下面我在 React 中使用 Typescript 时有两种类型检查道具的变体。
变化 1
interface Props {
prop1: string,
prop2: string,
}
const MyComponent = ({ prop1, prop2 }: Props) => {
return (
<></>
);
};
变体 2
interface Props {
prop1: string,
prop2: string,
}
const MyComponent: React.FC<Props> = (props) => {
const { prop1, prop2 } = props;
return (
<></>
);
};
以上哪些变体被认为是最佳实践,为什么?
【问题讨论】:
-
函数组件使用
FC<P>。它免费为您提供您没有想到的其他道具(例如children)。
标签: reactjs typescript typechecking react-typescript