Harsh Patel
Harsh Patel

Reputation: 6830

ReactJS "TypeError: Cannot read property 'array' of undefined"

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.

Upvotes: 14

Views: 19033

Answers (5)

shlok mukkawar
shlok mukkawar

Reputation: 26

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

Upvotes: 0

harun ugur
harun ugur

Reputation: 1852

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"
		}
}

Upvotes: 0

Nitin Singh
Nitin Singh

Reputation: 21

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
}`

Upvotes: 2

palsrealm
palsrealm

Reputation: 5243

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.

Upvotes: 0

Agney
Agney

Reputation: 19194

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

Upvotes: 33

Related Questions