Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第2章 报错 TypeError: Cannot read property 'string' of undefined #99

Open
lvyangzhuo opened this issue Jun 20, 2018 · 3 comments
Open

Comments

@lvyangzhuo
Copy link

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
Copy link
Author

lvyangzhuo commented Jun 20, 2018

自己解决了,附上原因:

在之前的版本之中,我们可以通过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,};
-- | --

@quankainwafu
Copy link

已解决,谢谢

@hiluo
Copy link

hiluo commented Jun 10, 2019

注意:

自 React v15.5 起,React.PropTypes 已移入另一个包中。请使用 prop-types 库 代替。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants