Uncaught TypeError: Cannot read property 'number' of undefined

引用以下内容时报错:
Uncaught TypeError: Cannot read property ‘number’ of undefined

static propTypes = {
    value: PropTypes.number.isRequired,
    onIncrement: PropTypes.func.isRequired,
    onDecrement: PropTypes.func.isRequired
  }

原因及解决:
在之前的版本之中,我们可以通过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,}; 

写法:
在组件内部写:

    static propTypes = {

        value: PropTypes.number.isRequired,
        onIncrement: PropTypes.func.isRequired,
        onDecrement: PropTypes.func.isRequired,
        onIncrementDoubble: PropTypes.func.isRequired,
        onDecrementDoubble: PropTypes.func.isRequired,
        incrementAsync: PropTypes.func.isRequired

    }

在组件外部写:

    ComponentChild.propTypes = {

        value: PropTypes.number.isRequired,
        onIncrement: PropTypes.func.isRequired,
        onDecrement: PropTypes.func.isRequired,
        onIncrementDoubble: PropTypes.func.isRequired,
        onDecrementDoubble: PropTypes.func.isRequired,
        incrementAsync: PropTypes.func.isRequired

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值