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.
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>
?
Component<Props>
is syntax of Flow to check type of data on Props. Flow infers types and tracks data as it moves through your code.
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:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.