最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJS code compiled successfully , but not printing on Browser - Stack Overflow

programmeradmin6浏览0评论

After a lot of efforts i was able to install and configure ReactJS. After executing "npm start" mand, the code was "COMPILED SUCCESSFULLY" and redirected me to the web browser. But there wasn't any output, ie., the browser was "blank". Can anyone resolve this?? ( my first post here )

Also check the code that i have used..

index.html file

<!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, { ponent } 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'
      })
   ]
}

The only issue is the output is not getting displayed on browser..

mand prompt COMMAND PROMPT AFTER "npm start"

browser output not displaying

After a lot of efforts i was able to install and configure ReactJS. After executing "npm start" mand, the code was "COMPILED SUCCESSFULLY" and redirected me to the web browser. But there wasn't any output, ie., the browser was "blank". Can anyone resolve this?? ( my first post here )

Also check the code that i have used..

index.html file

<!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, { ponent } 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'
      })
   ]
}

The only issue is the output is not getting displayed on browser..

mand prompt COMMAND PROMPT AFTER "npm start"

browser output not displaying

Share Improve this question edited Jul 16, 2019 at 7:44 Ed Bangga 13k4 gold badges17 silver badges30 bronze badges asked Jul 16, 2019 at 7:00 Rohit SawantRohit Sawant 111 gold badge1 silver badge3 bronze badges 1
  • Are there any errors in the browser's console? – Clarity Commented Jul 16, 2019 at 7:13
Add a ment  | 

3 Answers 3

Reset to default 2

Add an .babelrc file in root folder with following:

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

And package.json is expected to include following dependencies:

  "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"
  }

Update

Also update webpack.config.js to:

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"
    })
  ]
};

Check the path and change Component to ponent.

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

In ponent, when using the useState hook to manage the text state. Sometimes, an initial value for the state variable can be missing. When useState is called without an initial value, the state variable (text in this case) will be undefined initially. Eg:

const [text, setText] = useState("");
发布评论

评论列表(0)

  1. 暂无评论