gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:51:52 25 4
gpt4 key购买 nike

在运行此代码时,我在 App.propTypes 的第一行出现错误

TypeError: Cannot read property 'array' of undefined

代码:

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

我尝试搜索,但没有找到正确的解决方案。

最佳答案

Prop-Types 现在是一个单独维护的库,名为 prop-types以下是 react-docs 的解释:https://reactjs.org/docs/typechecking-with-proptypes.html

你必须将它们导入为

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

关于javascript - ReactJS "TypeError: Cannot read property ' array' of undefined",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com