在運行這段代碼我在App.propTypes
ReactJS「類型錯誤:無法讀取屬性的‘數組’未定義」
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"
}
}
我試圖尋找,但我沒有得到正確的解決方案。
您正在使用哪個版本的React? –
我猜你在React 16上,PropTypes已經移動到另一個名爲prop-types的包。 - https://reactjs.org/warnings/dont-call-proptypes.html –
@BoyWithSilverWings反應版本:16.0.0 –