NodeJS ReactJS代码编译成功,但未在浏览器上打印

ehxuflar  于 2023-06-29  发布在  Node.js
关注(0)|答案(3)|浏览(119)

经过大量的努力,我能够安装和配置ReactJS。执行“npm start”命令后,代码显示“COMPILED SUCCESSFULLY”,并将我重定向到Web浏览器。但没有任何输出,即浏览器为“空白”。有人能解决这个问题吗?(我的第一篇文章)
也检查我使用的代码。

index.html文件

<!DOCTYPE html>
<html lang = "en">
   <head>
      <meta charset = "UTF-8">
      <title>React App</title>
   </head>
   <body>
      <div id = "app"></div>
      <script src = 'index_bundle.js'></script>
   </body>
</html>

App.js

import React, { component } from 'react';
class App extends Component {
    render(){
        return(
        <div>
        <h1> Hello World</h1>
        <p>Hello </p>   
        </div>
        );
    }
}
export default App;

main.js

import React from 'react';
import { render } from 'react-dom';
import ReactDOM from 'react-dom';
import App from './App.js';
ReactDOM.render(<App />, document.getElementById('app'));

package.json snippet

"start": "webpack-dev-server --mode development --open --hot",
    "build": "webpack --mode production"

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
   entry: './main.js',
   output: {
      path: path.join(__dirname, '/bundle'),
      filename: 'index_bundle.js'
   },
   devServer: {
      inline: true,
      port: 8001
   },
   module: {
      rules: [
         {
            test: /\.jsx?$/,
            exclude: /node_modules/,
            loader: 'babel-loader',
            query: {
               presets: ['es2015', 'react']
            }
         }
      ]
   },
   plugins:[
      new HtmlWebpackPlugin({
         template: './index.html'
      })
   ]
}

唯一的问题是输出无法在浏览器上显示

命令提示符COMMAND PROMPT AFTER "npm start"
浏览器output not displaying

dohp0rv5

dohp0rv51#

在根文件夹中添加一个.babelrc文件,如下所示:

{
    "presets": ["@babel/preset-env", "@babel/preset-react"]
}

package.json预计将包含以下依赖项:

"devDependencies": {
    "@babel/core": "^7.4.0",
    "@babel/preset-env": "^7.4.2",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.5",
    "css-loader": "^2.1.1",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "react": "^16.8.5",
    "react-dom": "^16.8.5",
    "style-loader": "^0.23.1",
    "webpack": "^4.29.6",
    "webpack-cli": "^3.3.0",
    "webpack-dev-server": "^3.2.1"
  }

更新

同时将webpack.config.js更新为:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./main.js",
  output: {
    path: path.join(__dirname, "/bundle"),
    filename: "index_bundle.js"
  },
  devServer: {
    inline: true,
    port: 8001
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: "babel-loader"
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html"
    })
  ]
};
nimxete2

nimxete22#

检查路径并将Component更改为component

import React, { Component } from 'react';
class App extends Component {
  render() {
    return (
      <Router>
        <div className="App">
          <Route path="/" component={HomePage} exact  />
        </div>
      </Router> 
    );
  }
}
gz5pxeao

gz5pxeao3#

在组件中,当使用useState钩子来管理文本状态时。有时,状态变量初始值可能会丢失。当useState在没有初始值的情况下被调用时,状态变量(在本例中为文本)最初将是未定义的。例如:

const [text, setText] = useState("");

相关问题