我不知道何时应使用<>
以及何时应使用:
为属性分配类型。我下面的代码是正确的,但是我不确定为什么要声明React.FunctionComponent<Props>
而不是React.FunctionComonent : Props
interface Props {
speakers : Speaker[]
}
const SpeakersMain : React.FunctionComponent<Props> = (props : Props) => (
<div>
<SpeakersHeader/>
<SpeakerList speakers={props.speakers} />
</div>
);
const SpeakersMain: React.FunctionComponent<Props> = ...;
应该使用,因为使用:
afterReact.FunctionComponent
将是不正确的语法。
SpeakersMain: React.FunctionComponent
表示SpeakersMain
变量是React.FunctionComponent
类型。虽然<Props>
添加了澄清,React.FunctionComponent
因为在React类型中被定义为泛型类型。<>
语法允许Props
作为参数传递。
React.FunctionComponent<Props>
type表示它是一个将Props
类型作为其props
参数的函数。
它的工作方式是:
type Foo<T = {}> = (props: T) => void;
type Bar = { bar: number };
var foo1: Foo = (props) => { /* props is supposed to be empty object by default */ };
var foo2: Foo<Bar> = (props) => { /* props is supposed to be Bar */ };
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句