简体   繁体   English

`Component是什么意思 <Props> 反应本机

[英]What is the meaning of `Component<Props>` of react-native

I created a new react-native project recently, and I found the component syntax becoming export default class WelcomeScreen extends Component<Props> that's different from export default class WelcomeScreen extends Component before. 我最近创建了一个新的react-native项目,发现组件语法成为export default class WelcomeScreen extends Component<Props> ,这与之前export default class WelcomeScreen extends Component有所不同。

I thought it's replace the syntax of code of below 我以为它取代了下面代码的语法

  constructor(props) {
    super(props)
  }

but after testing, I found I still have to ref props with the code above, so what's the exactly function of this syntax <Props> ? 但是经过测试后,我发现我仍然必须使用上面的代码来引用props,那么此语法<Props>的确切功能是什么?

Component<Props> is syntax of Flow to check type of data on Props. Component<Props>是Flow的语法,用于检查Props上的数据类型。 Flow infers types and tracks data as it moves through your code. 当数据在代码中移动时,Flow会推断类型并跟踪数据。

Example: 例:

    // @flow
    import * as React from 'react';
    import { Text } from 'react-native';

    type Props = {
      bar: string, // this mean type data of bar is string and is required.
    };

    class MyComponent extends React.Component<Props> {
      render() {    
        return <Text>{this.props.bar}</Text>;
      }
    }

Reference: 参考:

https://flow.org/en/ https://flow.org/en/

https://flow.org/en/docs/react/ https://flow.org/en/docs/react/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM