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

组件里

  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,};


猜你喜欢

转载自blog.csdn.net/weixin_38049458/article/details/78847857