在学习react-native过程中,设置自定义组件 PropTypes遇到了个坑
import React, { Component , PropTypes} from 'react';
import {
Platform,
StyleSheet,
Text,
View
} from 'react-native';
export default class PropsTest extends Component<{}> {
static defaultProps={
name:'小红',
age:10
}
static propTypes={
name:PropTypes.string,
age:PropTypes.number,
sex:PropTypes.string.isRequired
}
render() {
return <View>
<Text style={{fontSize:20,backgroundColor:'red'}}>姓名:{this.props.name}</Text>
<Text style={{fontSize:20,backgroundColor:'red'}}>年龄:{this.props.age}</Text>
<Text style={{fontSize:20,backgroundColor:'red'}}>年龄:{this.props.sex}</Text>
</View>;
}
}
一直报错,找了N多方法,最后终于解决 import PropTypes from 'prop-types';