虽然已导入,但没有定义react16 PropTypes。使用webpack和babel进行配置

时间:2018-11-16 23:36:34

标签: reactjs webpack babel react-proptypes

我正在将项目从react 15迁移到react16。我更新了react和react-dom,然后将babel更改为babel @ 7,然后按照代码-mod标记了不安全的方法并将所有react.PropTypes更改为prop -types ...,但仍然以某种方式出现引用错误,提示未定义PropTypes。可能是我错过了一些配置吗?

import PropTypes from 'prop-types';
import React, { Component } from 'react';

export class Onboarding extends Component {
  static contextTypes = {
      store: PropTypes.object.isRequired
  };
  // rest of code...
}

但是它给出了Uncaught ReferenceError:未定义PropTypes

我正在使用webpack3。 webpack.config.js:

var path = require('path');
var webpack = require('webpack');
var BundleTracker = require('webpack-bundle-tracker');

module.exports = {
  context: __dirname,
  entry: ["@babel/polyfill",'./index'],

  output: {
    path: path.resolve('./static/js/dashboard/bundles/'),
    filename: 'dashboard-[hash].js',
  },

  plugins: [
    new BundleTracker({filename: './static/js/dashboard/webpack-stats.json'}),],

module: {
    loaders: [
        {test: /\.js?$/,
         exclude: /node_modules/,
         loader: 'babel-loader',
         query: {
            presets: ['@babel/preset-env']
            }
        },
    ]
},

resolve: {
    modules: ['node_modules'],
    extensions: ['.js', '.jsx']
  }   
}

和.babelrc

{
  "presets": [
    "@babel/preset-env", 
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties",
  ]
}

非常困惑正在发生的事情。还有我的package.json(这里没有配置存储,只有依赖项)

{
  ....
  "devDependencies": {
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"babel-loader": "^8.0.0",
"css-loader": "^0.28.7",
"jsdom": "^11.12.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-redux": "^5.0.5",
"react-router": "^3.0.5",
"react-router-redux": "^4.0.8",
"redux": "^3.6.0",
"uglify-js": "^1.3.5",
"uglifyjs-webpack-plugin": "^0.4.6",
"webpack": "^3.11.0",
"webpack-bundle-tracker": "^0.3.0",
"webpack-dev-server": "^3.1.10"
  },
  "dependencies": {
"@babel/polyfill": "^7.0.0",
"prop-types": "^15.6.2",
"redux-logger": "^3.0.6",
  }
}

感谢任何提示和建议!

0 个答案:

没有答案