14

While running this code I got error on the first line on App.propTypes

TypeError: Cannot read property 'array' of undefined

Code:

  class App extends React.Component {
   render() {
      return (
         <div>
            <h3>Array: {this.props.propArray}</h3>
            <h3>Array: {this.props.propBool ? "true" : "false"}</h3>
            <h3>Func: {this.props.propFunc(3)}</h3>
            <h3>Number: {this.props.propNumber}</h3>
            <h3>String: {this.props.propString}</h3>
            <h3>Object: {this.props.propObject.objectName1}</h3>
            <h3>Object: {this.props.propObject.objectName2}</h3>
            <h3>Object: {this.props.propObject.objectName3}</h3>
         </div>
      );
   }
}


App.propTypes = {
   propArray: React.PropTypes.array.isRequired, //I got error over here
   propBool: React.PropTypes.bool.isRequired,
   propFunc: React.PropTypes.func,
   propNumber: React.PropTypes.number,
   propString: React.PropTypes.string,
   propObject: React.PropTypes.object
}

App.defaultProps = {
   propArray: [1,2,3,4,5],
   propBool: true,
   propFunc: function(e){return e},
   propNumber: 1,
   propString: "String value...",

   propObject: {
      objectName1:"objectValue1",
      objectName2: "objectValue2",
      objectName3: "objectValue3"
   }
}

I tried to search but I didn't get the correct solution.

4
  • Which version of React are you using? Commented Oct 13, 2017 at 4:59
  • 1
    Im guessing your on React 16, PropTypes has been moved to another package called prop-types. - reactjs.org/warnings/dont-call-proptypes.html Commented Oct 13, 2017 at 5:06
  • @BoyWithSilverWings React version: 16.0.0 Commented Oct 13, 2017 at 5:08
  • 1
    Then, the answers have your solution Commented Oct 13, 2017 at 5:09

5 Answers 5

32

Prop-Types are now a separately maintained library named prop-types Here is the explanation from react-docs: https://reactjs.org/docs/typechecking-with-proptypes.html

You have to import them as

import React from 'react';
import PropTypes from 'prop-types'

class App extends React.Component {
  //App here
}

App.propTypes = {
 propArray: PropTypes.array.isRequired, 
 propBool: PropTypes.bool.isRequired,
 propFunc: PropTypes.func,
 propNumber: PropTypes.number,
 propString: PropTypes.string,
 propObject: PropTypes.object
}

NPM Package

Sign up to request clarification or add additional context in comments.

3 Comments

i also import PropTypes from 'prop-types'; but it didnt work
Yes, same error after adding import PropTypes from 'prop-types';
@WilliamKF remove React from App.propTypes. Your code become ` propArray: PropTypes.array.isRequired` and do same changes for every where
2

Change this:

App.propTypes = {
   propArray: React.PropTypes.array.isRequired, //I got error over here
   propBool: React.PropTypes.bool.isRequired,
   propFunc: React.PropTypes.func,
   propNumber: React.PropTypes.number,
   propString: React.PropTypes.string,
   propObject: React.PropTypes.object
}

to:

App.propTypes = {
  propArray: PropTypes.array.isRequired,
  propBool: PropTypes.bool.isRequired,
  propFunc: PropTypes.func,
  propNumber: PropTypes.number,
  propString: PropTypes.string,
  propObject: PropTypes.object
}`

Comments

0

The React package no longer contains PropTypes. You need to install the prop-types package and

import PropTypes from 'prop-types';

Edit: As stated in the first paragraph in the documentation

React.PropTypes has moved into a different package since React v15.5. Please use the prop-types library instead.

Comments

0

You should change React.PropTypes.array.* to PropTypes.array.*

App.propTypes = {
   propArray: PropTypes.array.isRequired,
   propBool: PropTypes.bool.isRequired,
   propFunc: PropTypes.func,
   propNumber: PropTypes.number,
   propString: PropTypes.string,
   propObject: PropTypes.object,
	 headerProp: PropTypes.string,
	 contentProp: PropTypes.string
}
App.defaultProps = {
		headerProp: "Header from props...",
		contentProp:"Content from props...",
		propArray: [1,2,3,4,5],
		propBool: true,
		propFunc: function(e){return e},
		propNumber: 1,
		propString: "String value...",

		propObject: {
			objectName1:"objectValue1",
			objectName2: "objectValue2",
			objectName3: "objectValue3"
		}
}

Comments

0

So, It can happen in a lot of dependecies which are using React.PropTypes you can resolve this by importing

import PropTypes from 'prop-types'

and after that change React.PropType to just PropType.It happens because PropType is not default in react anymore you need to import it seperately.

Before cards: React.PropTypes.array.isRequired, After cards: PropTypes.array.isRequired,

Error I encountered which got resolved when i did this enter image description here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.