React 中的尖括号和星号是什么意思? [英] What do the angle brackets and asterisk mean in React?
问题描述
这个表达式中的尖括号和星号是什么意思?
What do the angle brackets and asterisk mean in this expression?
class MainScreen extends React.Component<*> {
render() {
推荐答案
这是一个 Flow 类型参数.它指定 Flow 应该推断第一个类型参数的类型,换句话说,推断道具的形状,参见 这个答案在<*>
.这基本上让您可以为 Flow 指定任何形状的对象以推断其类型.假设你有两个 props,foo
和 bar
.你可以这样做:
It's a Flow type argument. It specifies that Flow should infer the type of the first type argument, in other words infer the shape of props, see this answer on <*>
. This essentially let's you specify an object of any shape for Flow to infer its type. Say you have two props, foo
and bar
. You could do this:
type Props = {
foo: number,
bar: string
};
class MyComponent extends React.Component<Props> { … }
或者,如果您不想进行proptyping,您可以这样做:
Or, if you don't want proptyping, you can just do:
class MyComponent extends React.Component<*> { … }
并且访问道具都是一样的.Flow 只是推断它是一个带有 foo
和 bar
的对象.
And access the props all the same. Flow just infers it's an object with foo
and bar
.
这篇关于React 中的尖括号和星号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!