React 16.7 - React.SFC现已弃用

Jon*_*aem 42 deprecated typescript reactjs deprecation-warning

我用来声明像这样的无状态组件:

const example: React.SFC<IExample> = ({propsType}) => ();
Run Code Online (Sandbox Code Playgroud)

然而,证监会现已被弃用,也许Dan Abramov的这篇推文解释了原因.

现在SFC被弃用了我们应该使用什么?

Doğ*_*acı 69

你应该使用React.FunctionComponent:https://github.com/DefinitelyTyped/DefinitelyTyped/pull/30364

这PR重命名React.SFC,并React.StatelessComponentReact.FunctionComponent,同时引入弃用别名旧名称.

所以你的例子将成为:

const example: React.FunctionComponent<IExample> = ({propsType}) => ();
Run Code Online (Sandbox Code Playgroud)

要么

const example: React.FC<IExample> = ({propsType}) => ();
Run Code Online (Sandbox Code Playgroud)


pan*_*ter 11

我想说接受的答案不再是最新的。

React.FunctionComponent正如 create-react-app 的人员所解释的那样,它有某些缺点。他们的推理很有说服力,我FC完全停止使用了。

更好的选择:

const example = ({ propsType }: IExample): JSX.Element => ();
Run Code Online (Sandbox Code Playgroud)