javascript – _react2.default.PropTypes.function未定义

前端之家收集整理的这篇文章主要介绍了javascript – _react2.default.PropTypes.function未定义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有一个Button组件
import React,{ Component } from 'react';
import { View,Text,TouchableNativeFeedback } from 'react-native';

class Button extends Component {
    generateComponent() {
        const { buttonStyle,textStyle } = this.styles;
        const { text } = this.props;

        switch (this.props.platform) {
            case 'android':
                return (
                    <TouchableNativeFeedback onPress={this.props.onPress}>
                        <View style={buttonStyle}>
                            <Text style={textStyle}>{text}</Text>
                        </View>
                    </TouchableNativeFeedback>
                );
            case 'ios':
                return 0;
        }
    }

    render() {
        return (
            <View>
                { this.generateComponent() }
            </View>
        );
    }

    styles = {
        buttonStyle: {
            borderRadius: 100,justifyContent: 'center',height: parseInt(this.props.size,10),width: parseInt(this.props.size,backgroundColor: this.props.color,elevation: 3
        },textStyle: {
            fontWeight: 'bold',fontSize: parseInt(this.props.fontSize,lineHeight: parseInt(this.props.fontSize,10)
                + Math.floor(parseInt(this.props.fontSize,10) / 10) + 1,color: this.props.fontColor,textAlign: 'center'
        }
    }
}

Button.propTypes = {
    text: React.PropTypes.string.isrequired,platform: React.PropTypes.string.isrequired,size: React.PropTypes.string.isrequired,color: React.PropTypes.string.isrequired,fontColor: React.PropTypes.string.isrequired,fontSize: React.PropTypes.string.isrequired,onPress: React.PropTypes.function.isrequired
};

export default Button;

我在组件Home中调用此组件

import React,{ Component } from 'react';
import { View } from 'react-native';
import Metas from '../components/Metas';
import Button from '../components/Button';

export default class Home extends Component {
    constructor(props) {
        super(props);
        this.state = { Metas: ['Meta 1','Meta 2','Meta 3'] };
    }

    render() {
        return (
            <View>
                <Metas data={ this.state.Metas } />
                <Button
                    text="+"
                    platform={ this.props.platform }
                    onPress={ this._handleButtonPress }
                    size='50'
                    fontSize='25'
                    color='#FFD600'
                    fontColor='white'
                />
            </View>
        );
    }

    _handleButtonPress() {
        console.log("Hello!");
    }
}

Home.propTypes = {
    platform: React.PropTypes.string.isrequired
};

问题是,如果我包含PropTypes,我会收到一个错误,指出onPress道具未定义.

我不明白为什么我收到这个错误.我安慰记录了onPress的typeof并打印功能.

有什么建议??

解决方法

更新于2017年9月

从React 15.5开始,PropTypes已被移动到它自己的库中.像这样用它:

import PropTypes from 'prop-types';

class Example extends React.Component {
  render() {
    return (
      <h1>{this.props.test}</h1>
    );
  }
}

Example.propTypes = {
  test: PropTypes.string
};

资料来源:https://facebook.github.io/react/docs/typechecking-with-proptypes.html

原文链接:https://www.f2er.com/js/159605.html

猜你在找的JavaScript相关文章