无法读取未定义的 属性 'string' | React.PropTypes

Cannot read property 'string' of undefined | React.PropTypes

我通过 运行 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;

我重新启动了节点 5-6 次,但仍然出现此错误:

我在这里错过了什么?

基于 ReactJS documentation/tutorial 页面,Type Checking with PropTypes,看起来您正在尝试使用旧方法访问 PropTypes

试试改用这个:

Question.propTypes = {
    content: PropTypes.string.isRequired
};

查看 documentation 您不需要 React 前缀:

Question.propTypes = {
  content: PropTypes.string.isRequired
};