Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

_react2.default.PropTypes.function is undefined

So I have a Button component

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, 10),
            backgroundColor: this.props.color,
            elevation: 3
        },
        textStyle: {
            fontWeight: 'bold',
            fontSize: parseInt(this.props.fontSize, 10),
            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;

And I call this component in the component 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
};

The problem is that if I include the PropTypes I get an error saying that the onPress prop is undefined.

I do not understand why I recieve this error. I consoled logged the typeof onPress and it prints function.

Any suggestion??

like image 629
Goamaral Avatar asked Mar 01 '17 21:03

Goamaral


2 Answers

UPDATE SEPTEMBER 2017

As of React 15.5 PropTypes has been moved to it's own library. Use it like this:

import PropTypes from 'prop-types';

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

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

Source: https://facebook.github.io/react/docs/typechecking-with-proptypes.html

like image 75
pucky124 Avatar answered Oct 19 '22 12:10

pucky124


You have mistyped the prop type. It should be

onPress: React.PropTypes.func

Please see this reference - https://facebook.github.io/react/docs/typechecking-with-proptypes.html

like image 3
curv Avatar answered Oct 19 '22 12:10

curv