【发布时间】:2020-05-03 14:47:19
【问题描述】:
如何使用 typescript 在 react 中创建功能组件的正确方法是什么?
- 我应该使用
interface还是输入props? - 我应该使用
React.FC还是React.FunctionComponent? - 如何使用
eslint验证props?
现在,我的典型组件如下所示:
interface IProps {
color: string;
}
const Example = (props: IProps) => {
const { color } = props;
return (
<>
{color}
</>
);
};
我不确定这是否是最好的方法......
我也没有t know how to validate props usingeslint`,例如当我想将颜色作为数字传递时..
【问题讨论】:
-
React.FC 和 React.FunctionComponent 是一样的。
-
你的代码就差不多了。您只需将
const Example = (props: IProps) => {更改为const Example: React.FC<IProps> = (props) => {。另外,你的界面很好!
标签: javascript reactjs typescript eslint