0

I am trying to learn to react, after spending two days on setting up webpack and babel. Finally, I am trying to run some sample code. I am trying to print some strings from my react element, and unable to get this element working.

I do get the "Hello World" which is from the HTML, and no compilation errors for react, so I can validate client-server setup is working well.

However, the react element is not rendered.

Following is three file setup.

components/homepage.js

"use strict";

var React = require('react');

var Home = React.createClass({
    render: function () {
        return (
        <div className="jumbotron">
            <h1>PluralSight Adminstrator</h1>
            <p>React, React Router, and Flux for ultra responsive website</p>
        </div>
        );
    }
});

module.exports = Home;

And index.js

const jquery = $ = require('jquery');
const Home = require('./components/homepage');
const ReactDOM = require('react-dom');
ReactDOM.render(<Home/>, document.getElementById('app'));

And index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="index.js"></script>

</head>
<body>
    <div id="app"></div>
    Hello World.:)
</body>
</html>

webpack.config.dev.js

const webpack = require('webpack');
const path = require('path');

module.exports = {
    debug: true,
    devtool: 'inline-source-map',
    noInfo: false,
    entry: [
        'eventsource-polyfill', // necessary for hot reloading with IE
        'webpack-hot-middleware/client?reload=true', //note that it reloads the page if hot module reloading fails.
        path.resolve(__dirname, 'src/index')
    ],
    target: 'web',
    output: {
        path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
        publicPath: '/',
        filename: 'bundle.js'
    },
    devServer: {
        contentBase: path.resolve(__dirname, 'src')
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin()
    ],
    module: {
        loaders: [
            {test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
            {test: /(\.css)$/, loaders: ['style', 'css']},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
            {test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
            {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}
        ]
    }
};
1
  • 1
    I'd recommend following a more recent tutorial. Commented Jun 17, 2018 at 22:26

1 Answer 1

2

The following code should work (replace the home component code):

var Home = class Home extends React.Component {
    render() {
        return (
        <div className="jumbotron">
            <h1>PluralSight Adminstrator</h1>
            <p>React, React Router, and Flux for ultra responsive website</p>
        </div>
        );
    }
}

According to docs, if you want to use create-react-class then you need to install the package using NPM and require it as shown below:

var createReactClass = require('create-react-class');

The compiler might not show the error, but your browser's console must be showing some error.

Ref fiddle: https://jsfiddle.net/69z2wepo/194263/

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

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.