第2章 报错 TypeError: Cannot read property 'string' of undefined
lvyangzhuo opened this issue · comments
lvyangzhuo commented
TypeError: Cannot read property 'string' of undefined
(anonymous function)
D:/Java/workspace-neon/suibian-manger-react/src/Counter.js:49
46 | }
47 |
48 | Counter.propTypes = {
49 | caption: PropTypes.string.isRequired,
50 | initValue: PropTypes.number,
51 | onUpdate: PropTypes.func
52 | };
View compiled
▶ 12 stack frames were collapsed.
lvyangzhuo commented
自己解决了,附上原因:
在之前的版本之中,我们可以通过React.PropTypes这个API访问React内置的一些类型来检查props,在15.5.0版本中,这一API被独立成了一个新的包 prop-types
// 15.4 以前
import React from 'react';
class Component extends React.Component {
render() { return
{this.props.text}
; }} Component.propTypes = { text: React.PropTypes.string.isRequired,}
// 15.5 以后
import React from 'react';
import PropTypes from 'prop-types';
class Component extends React.Component { render() { return
{this.props.text}
; }}Component.propTypes = { text: PropTypes.string.isRequired,};-- | --
quxiusu commented
已解决,谢谢
Eric Luo commented
注意:
自 React v15.5 起,React.PropTypes 已移入另一个包中。请使用 prop-types 库 代替。