1

After a lot of efforts i was able to install and configure ReactJS. After executing "npm start" command, 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, { 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'
      })
   ]
}

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

command prompt COMMAND PROMPT AFTER "npm start"

browser output not displaying

1
  • Are there any errors in the browser's console? Commented Jul 16, 2019 at 7:13

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

,babelrc already exists... the code and everything is correct , but not getting why its not executing
Can you share your code to check coz exact above code did work for me.
0

Check the path and change Component to component.

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

Comments

0

In component, 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("");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.