undefined is not an object (evaluating '_react3.default.PropTypes.bool')

在学习React Native的过程中,也就是学习生命周期defaultProps()的时候,为defaultProps配置属性和为属性申请类型时出现错误

  static defaultProps = {
        autoPlay: false,
        maxLoops: 10,
        nameString:'suncuihua',
    };  // 注意这里有分号

    static propTypes = {

        autoPlay: PropTypes.bool.isRequired,
        maxLoops: PropTypes.number.isRequired,
        posterFrameSrc:PropTypes.string.isRequired,
        videoSrc: PropTypes.string.isRequired,
        nameString: PropTypes.string.isRequired,

    };  // 注意这里有分号

报错:



解决办法:

需要我们安装软件包prop-types 安装方法为:

npm install prop-types --save      //安装prop-types


加入  import { PropTypes} from 'prop-types';


注释:从React15.5起,React.PropTypes被移入到单独的package中。react提供了一个package(prop-types)去检查props的类型。首先需要将prop-types引用到文件中。 


引入方法:
static propTypes = {
        autoPlay: PropTypes.bool.isRequired,
        maxLoops: PropTypes.number.isRequired,
        posterFrameSrc:PropTypes.string.isRequired,
        videoSrc: PropTypes.string.isRequired,
        nameString: PropTypes.string.isRequired,
    };  // 注意这里有分号


完成之后可能会报错,不报错就不用管了:





解决方法:
安装 npm-check-updates 这个包:
npm i -g npm-check-updates


在项目目录下

rm -rf node_modules
ncu -u
npm install
此方法可以解决很多莫名其妙的 node 包依赖问题

猜你喜欢

转载自blog.csdn.net/sun_cui_hua/article/details/79879603