3

Recently the urge to try react with express has landed me to a common issue and being new to it leaving me clueless.

I have a react express application with structure:

+-- app.js
+-- public
|   +-- css
    +-- javascripts
+-- routes
|   +-- index.js
+-- views
|   +-- index.jsx
    +-- layouts
        +-- Default.jsx

Taking the baby steps for rendering the view with default stylesheets and scripts (Default layout file--> Default.jsx)

    var React = require('react');

    class DefaultLayout extends React.Component {
      render() {
        return (
         <html><head><title>{this.props.title}</title>
      <link href="stylesheets/style.css"  rel="stylesheet" />
    </head>
    <body>
    First Application {this.props.children}
    </body>
    <script src="javascripts/jquery-2.2.4.min.js"></script>
    </html>
  );
  }
}

module.exports = DefaultLayout;

Index.jsx

  var React = require('react');
    var DefaultLayout = require('./layouts/default');

    class FirstPage extends React.Component {
      render() {
        return (
      <DefaultLayout title={this.props.title}>
      </DefaultLayout>
        );
      }
    }
module.exports = FirstPage;

Routes>index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { name: 'Ankita',title:'First Page' });
});


module.exports = router;

The First Page is getting rendered but the scripts seems to be not doing its work. I'm receiving the warning Warning: validateDOMNesting(...): cannot appear as a child of . See DefaultLayout > html > script.

My app.js

var express = require('express');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jsx');
app.engine('jsx', require('express-react-views').createEngine());
...
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
module.exports = app;

How do we include common javascript the way we include css files?

Ankita Goyal
  • 391
  • 1
  • 2
  • 16
  • I'm not sure but try changing these lines: `app.use(express.static(__dirname + '/public'));` and `src="/javascripts/jquery-2.2.4.min.js"` `href="/stylesheets/style.css"` – Rui Costa Jun 09 '17 at 14:54
  • You also have a typo `First Application {this.props.childern}` should be `children` – RickTakes Jun 09 '17 at 15:53
  • @RickTakes That is a typo error here. It's right there in program. – Ankita Goyal Jun 09 '17 at 16:51
  • @RuiCosta No luck! And only my style.css is getting imported which was generated using express-generator. All the files are giving back 304 response and not rendering in html. – Ankita Goyal Jun 09 '17 at 17:06
  • You got a few problems here. First React is recommended as an *Single Page Application* (spa) framework while you're using it in a multipage way; Second `jsx` isn't the same as `html`, it's essentially JavaScript, so you shoud change your `DefaultLayout` to be an `html`/`pug` template language and use `jsx` only for React Components. – Allen Jun 09 '17 at 17:24
  • @Xlee My content would keep changing according to url but it would be rendering on the same page so it will be spa i guess. And if i define DefaultLayout to be html then using browserify and stringify would be the right way to use? – Ankita Goyal Jun 09 '17 at 17:52
  • @AnkitaGoyal Your _project structure_ (which is the default of some Express app generator) implies a multipage app. If you want to go with SPA, you should move all `views` directory into `public/javascript` and use some module bundling tool like `webpack`, `public/javascript` becomes the **root** directory of your React app (though it's more of a convention thing) – Allen Jun 09 '17 at 18:13
  • @AnkitaGoyal If you want to stick to multipage, then consider use `pug` as templating language as `jsx` isn't a templating language for server side rendering. – Allen Jun 09 '17 at 18:14
  • @Xlee Thank you for the help. I'm targeting to design simple page application. Can you explain the reason of shifting the views? And also the main issue is to include the js and css files in the DefaultLayout? How do i do it? – Ankita Goyal Jun 09 '17 at 18:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146294/discussion-between-xlee-and-ankita-goyal). – Allen Jun 09 '17 at 19:01
  • @loganfsmyth Hello Logan! I just came across a link https://stackoverflow.com/questions/34424845/adding-script-tag-to-react-jsx where you commented. I'm trying to load JS files from Base html written in .jsx file, File is loaded but not working out. – Ankita Goyal Jun 10 '17 at 08:01

0 Answers0