javascript ReactJS“类型错误:无法读取未定义的属性‘数组’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46722788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReactJS "TypeError: Cannot read property 'array' of undefined"
提问by Harsh Patel
While running this code I got error on the first line on App.propTypes
运行此代码时,我在第一行出现错误 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.
我试图搜索,但没有得到正确的解决方案。
回答by Agney
Prop-Types are now a separately maintained library named prop-typesHere is the explanation from react-docs: https://reactjs.org/docs/typechecking-with-proptypes.html
Prop-Types 现在是一个单独维护的库,名为prop-types这里是 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
}
回答by Nitin Singh
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
}`
回答by harun ugur
You should change React.PropTypes.array.* to PropTypes.array.*
您应该将 React.PropTypes.array.* 更改为 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"
}
}
回答by palsrealm
The React package no longer contains PropTypes. You need to install the prop-typespackage and
React 包不再包含 PropTypes。您需要安装该prop-types软件包并
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.
自 React v15.5 以来,React.PropTypes 已移入不同的包中。请改用 prop-types 库。

