0

I'd like to use Express, pg and react for my project. But react give me some problems.

Here's my project's dir

index.js

var express = require('express');
var server = express();
var path = require('path');
var app = require('./app/app.js');

server.get('/', function (req, res) {
    res.sendFile(path.join(__dirname + '/public/index.html'));
});
server.listen(3000, function () {
    console.log("server is running on port 3000!");
});

var connexionDB = require('./db/connexionAvecPg');

app.js

var react = require('react');
var {render} = require('react-dom');

var App = react.createClass({
    render: function () {
        return(
                <div>
                    <p>test</p>
                </div>
                );
    }
});

render(
        <App/>,
        document.getElementById('root')
        );

(Yes it's ES5, but i have my reason for using it instead of ES6-7)

Here's my bug

What's wrong with my code? did i forget something? Thx for your time.

---\ EDIT /---

After Ming Soon's answer, I tried this:

app.js

var React = require('react');
var ReactDOM = require('react-dom');

class App extends React.component{
    render() {
        return React.createElement('div', null, 'Test');
    }
};

ReactDOM.render(
        React.createElement(App, null),
        document.getElementById('root')
        );

But i have this:

/home/josue/im-expressandpg/app/app.js:14
class App extends React.component{
                       ^

TypeError: Class extends value undefined is not a constructor or null

---\ EDIT 2 /---

Thx to PSo i saw my mistake. I changed this:

class App extends React.component{

To this:

class App extends React.Component{

When i exec:

/home/josue/im-expressandpg/app/app.js:17
        document.getElementById('root')
        ^

ReferenceError: document is not defined

If i add this:

var document = require('./../public/index.html');

I get:

/home/josue/im-expressandpg/public/index.html:1
(function (exports, require, module, __filename, __dirname) { <!doctype html>
                                                              ^
SyntaxError: Unexpected token <
1
  • use capital letter C for :React.Component Commented Feb 21, 2017 at 10:21

2 Answers 2

1

To use the JSX syntax, you have to transpile the code. Babel can be used as a transpiler. If you don't want to use any transpiler, then you should use react.createElement() function.

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

2 Comments

I did few modifications, can you see with me?
Without ES6, use react.createClass to define a component as you did at first. facebook.github.io/react/docs/react-without-es6.html
0

Remove the lower part of the code, that's duplicate of render function. And remember to export it for later use.

var React = require('react');
var ReactDOM = require('react-dom');

class App extends React.Component{
    render() {
        return React.createElement('div', null, 'Test');
    }
};

export default App;

1 Comment

But I want only one React file (for the moment). I have to ReactDOM.render it somewhere, right? thx for your help.

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.