javascript - 未定义错误 'propTypes' 未定义错误 'defaultProps'

标签 javascript reactjs eslint react-proptypes

我正在处理 react-redux 应用程序的一些可访问性问题。

我安装了 npm install eslint eslint-plugin-jsx-a11y@4.0.0 --save-dev 现在,当我运行我的应用程序时,出现以下错误:

Failed to compile.

Error in ./src/components/Promo.jsx

/code/src/components/Promo.jsx
  15:10  error  'propTypes' is not defined       no-undef
  19:10  error  'defaultProps' is not defined    no-undef
  42:3   error  'updateCellCode' is not defined  no-undef

✖ 3 problems (3 errors, 0 warnings)

我没有看到任何语法错误,代码对我来说是正确的:

import React, { PureComponent, PropTypes } from 'react';
import { throttle, prepAndFormatForInnerHTML } from '../util';
import '../assets/styles/application/promo.css';
import Promos from '../assets/strings/promos.json'; // placeholder info

const MOBILE_BREAKPOINT = 679; // from scss file

/** Used in Account, App, and OrderStatus */
class Promo extends PureComponent {
  static propTypes = {
    hideOnSmall: PropTypes.bool,
  }

  static defaultProps = {
    hideOnSmall: false,
  }

  constructor() {
    super();

    this.updateCellCode = throttle(this.updateCellCode, 100);

    this.state = {
      cellCode: Promos[0].cellCodeWeb,
    };
  }

其他人能看出这里有什么问题吗?为什么会出现这些错误?

最佳答案

PropTypes 现在是 separate包(自 React v15.5 起)。

所以代替

import { PropTypes } from 'react';

请使用

import PropTypes from 'prop-types';

实际错误如何 - 您可以尝试使用版本 3eslint(关于此的 see more)。

关于javascript - 未定义错误 'propTypes' 未定义错误 'defaultProps',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52055475/

相关文章:

javascript - ES6使用模块调用另一个类中的方法

javascript - 如何在 ReactJS 中使用加载图标更改步骤编号

reactjs - 有没有来自react-router的withRouter的替代品

javascript - 函数参数中每行的最大 jsx Prop

javascript - 使用断言 Expect(xhr.requestBody.test).to.not.exist 运行 cypress 测试用例,但 eslint 抛出错误

javascript - eslint/ typescript : Unable to resolve path to module

javascript - 当我尝试插入它时,firebase.firestore.FieldValue.serverTimestamp() 创建了一个 "modified"快照条目

Javascript 与高度取反模数

javascript - reactjs 中的反模式到底是什么?

reactjs - 是否可以在 React Hooks 中对 useReducer 的调度进行回调?