简体   繁体   English

React 组件未加载 Webpack 和 Babel 初学者级别

[英]React Component not loading with Webpack and Babel Beginner's level

I am trying to understand how React, Babel and Webpack interact with each other, but I get the following error: Uncaught TypeError: Super expression must either be null or a function.我试图了解 React、Babel 和 Webpack 如何相互交互,但出现以下错误:未捕获的类型错误:超级表达式必须为空或函数。 The CSS is rendering just fine but the HTML isn't, though I was able to see it in the console (view image below). CSS 呈现得很好,但 HTML 不是,尽管我能够在控制台中看到它(查看下图)。 Any suggestions?有什么建议?

package.json包.json

{
  "name": "react-raw",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "babel": {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ]
  },
  "scripts": {
    "start": "webpack-dev-server --open"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^16.8.4",
    "react-dom": "^16.8.4"
  },
  "devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.1",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.1",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  }
}

index.js索引.js

var React = require("react");
var ReactDOM = require("react-dom");

require("./index.css");

class App extends React.Component() {
  render() {
    return <div>Hello Christian!!</div>;
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

webpack.config.js webpack.config.js

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin"); //installed via npm
//const webpack = require("webpack"); //to access built-in plugins

module.exports = {
  entry: "./app/index.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "index_bundle.js"
  },
  module: {
    rules: [
      { test: /\.(js)$/, use: "babel-loader" },
      { test: /\.css$/, use: ["style-loader", "css-loader"] }
    ]
  },
  mode: "development",
  plugins: [new HtmlWebpackPlugin({ template: "app/index.html" })]
};

index.html索引.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>React Raw</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

类型错误

元素被渲染

在此处输入图片说明

Your problem is with the signature of your class component:您的问题在于您的class组件的签名:

class App extends React.Component() {
  render() {
    return <div>Hello Christian!!</div>;
  }
}

This is how you need to declare it:这是您需要声明的方式:

class App extends React.Component{
  render() {
    return <div>Hello Christian!!</div>;
  }
}

Note the extra () i omitted注意额外的()我省略了

You can read more about classes in here您可以在此处阅读有关课程的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM