react项目编译不过去,'propTypes' is not defined no-undef

问题描述

报'propTypes' is not defined no-undef 错误

问题出现的环境背景及自己尝试过哪些方法

安装了propTypes,也引入了,但是不知道为什么报这个错

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
import React from 'react';
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom';
import { hashHistory } from 'react-router';
import NotFound from './components/pages/NotFound';
import App from './App';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import {initMenu} from './store/index/action'

class Page extends React.Component {

static propTypes = {
    initMenu: PropTypes.func
}
constructor(props){
    super(props);
    this.state={
        path: '/app/realtime/survey'
    }
}

你期待的结果是什么?实际看到的错误信息又是什么?

阅读 8.2k
2 个回答

在Page类外面写

class Page {
    ...
}
Page.propTypes = {}

楼上@基尼尔凯 的做法是可以的。

你这种写法目前来看是不支持的,不是标准,如果想用的话需要babel转义。

React 官方原文:

If you are using a Babel transform like transform-class-properties , you can also declare defaultProps as static property within a React component class. This syntax has not yet been finalized though and will require a compilation step to work within a browser. For more information, see the class fields proposal.

详情查看: https://reactjs.org/docs/type...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题