React.PropTypes.number控制台报错说number是undefined

来源:互联网 发布:苹果手机日志软件 编辑:程序博客网 时间:2024/05/16 18:59

组件里

  BodyIndex.propTypes = {

    usernum: React.PropTypes.number

  };

控制台报错显示的是:Cannot read property 'number' of undefined


在之前的版本之中,我们可以通过React.PropTypes这个API访问React内置的一些类型来检查props,在15.5.0版本中,这一API被独立成了一个新的包 prop-types

// 15.4 以前import React from 'react';class Component extends React.Component {  render() {      return <div>{this.props.text}</div>;        }} Component.propTypes = {  text: React.PropTypes.string.isRequired,}// 15.5 以后import React from 'react';import PropTypes from 'prop-types';class Component extends React.Component {  render() {      return <div>{this.props.text}</div>;        }}Component.propTypes = {  text: PropTypes.string.isRequired,};


原创粉丝点击