无法读取未定义的属性“字符串" |React.PropTypes [英] Cannot read property 'string' of undefined | React.PropTypes

查看:61
本文介绍了无法读取未定义的属性“字符串" |React.PropTypes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过运行 npm i prop-types --save 安装了 prop-types 包,我的依赖项是:

I installed the prop-types package by running npm i prop-types --save and my dependencies are:

 "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 次,但仍然出现此错误:

I restarted node 5-6 times but still getting this error:

我在这里遗漏了什么?

推荐答案

基于 ReactJS 文档/教程页面,使用 PropTypes 进行类型检查,看起来您正在尝试使用旧方法访问 PropTypes.

Based on a ReactJS documentation/tutorial page, Type Checking with PropTypes, it looks like you are attempting to access PropTypes using the old approach.

尝试改用这个:

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

这篇关于无法读取未定义的属性“字符串" |React.PropTypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆