2

I've been following a React guide which goes through setting up webpack and babel in my repo. I created a react element which will simply print out "Hello World", and then I use the ReactDOM.render method to add this element into the index.html file, but for some reason it's not being added in.

Here is my Index.js file:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

export default class App extends React.component {
    render() {
        return(
            <div>
              Hello World
            </div>
        )
    }
};
//////EDITED OUT <button /> for <App />
ReactDOM.render(<App />, document.getElementById("root"))

My index.html file:

<!DOCTYPE html>
<div id="root"></div>

My webpack.config.js file

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

module.exports = {
    entry: "./App/index.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js"
    },
    module: {
        loaders: [
            {
                test: /\.(js)$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
            },
            { test: /\.css$/, use: [ 'style-loader', 'css-loader' ]}
        ]
     },
     plugins: [
          new HtmlWebpackPlugin({
              template: 'App/index.html'
          })
      ]
}

My index.css:

body {
  background: red;
}

My .babelrc file

{
  "presets": ["es2015", "react"]
}

I've checked the dev console on my browser and it simply isn't showing the component enclosed inside the component in index.html file. Is there something additional I need for ReactDOM to push my react element into the DOM?

I've looked at others similar questions on stack but their problems ended up existing because of a typo, I've strained my eyes looking over my code and can't find any typos. I think there may be a step in the installation that I've skipped.

1 Answer 1

5

You need to render <App /> not <button />

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

export default class App extends React.component {
    render() {
        return(
            <div>
              Hello World
            </div>
        )
    }
};
document.createelement("LI").appendChild(document.createTextNode("Water"));
ReactDOM.render(<App />, document.getElementById("root"))

You also need to output that

/dist/bundle.js

in index.html

like so

<!DOCTYPE html>
<div id="root"></div>
<script src="/dist/bundle.js"></script>
Sign up to request clarification or add additional context in comments.

6 Comments

Whoops, I added that in when I was testing to see if ReactDOM could render any element, turns out it can't. The problem still persists when I change it to <App />
Ok and what script tag you have included on your page? I dont see any in index.html. I updated my answer for that as well.
The dev console shows the HTML as: <body><div id="root"></div><script type="text/javascript" src="bundle.js"></body>. I think the script tag appeared because of webpack, I'm not sure
I tried adding in a script tag inside the root div tags in html, as <script type="text/babel" src="index.js"></script>. Still the react component doesn't appear
Webpacks output is under /dist. so you need (whatever your root is) /dist/bundle.js
|

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.