我通过运行 npm i prop-types --save
安装了 prop-types
包,我的依赖项是:
"dependencies": {
"prop-types": "^15.7.2",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-scripts": "3.0.1"
},
我的代码:
import React from 'react';
import PropTypes from 'prop-types';
function Question(props) {
return (
<h2 className="question">{props.content}</h2>
);
}
Question.propTypes = {
content: React.PropTypes.string.isRequired
};
export default Question;
我在这里缺少什么?
最佳答案
基于 ReactJS 文档/教程页面,Type Checking with PropTypes ,您似乎正在尝试使用旧方法访问 PropTypes
。
尝试使用这个:
Question.propTypes = {
content: PropTypes.string.isRequired
};
关于javascript - 无法读取未定义的属性 'string' | React.PropTypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57639972/