Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS Invalid Checksum

Tags:

reactjs

I keep getting the following error when attempting server side rendering using ReactJS & node.

React attempted to use reuse markup in a container but the checksum was invalid.

I've seen an answer that passing the same props on the server and client resolves this issue. In my example, I don't have any props, so I'm not sure that the answer applies to my problem.

You can view my full example on my github account.

I'm including the important code below. Any insight is greatly appreciated.

JSX

/** @jsx React.DOM */
var React = require('react');
var index = React.createClass({
    render: function() {
        return (
          <html>
          <head>
            <script src="bundle.js"></script>
          </head>
          <body>
            <div>
              hello
           </div>
          </body>
         </html>
     );
   }
});

if (typeof window !== "undefined") {
  React.renderComponent(<index/>, document.documentElement);
} else {
  module.exports = index;
}

Server

require('node-jsx').install();
var express = require('express'),
    app     = express(),
    React = require('react'),
    index = require('./index.jsx');

var render = function(req, res){
  var component = new index();
  var str = React.renderComponentToString(component);
  res.set('Content-Type', 'text/html');
  res.send(str);
  res.end();
}

app.get('/',render);
app.use(express.static(__dirname));

app.listen(8080);
like image 929
Nael Avatar asked Oct 03 '14 02:10

Nael


1 Answers

Change

React.renderComponent(<index/>, document.documentElement);

to

React.renderComponent(<index/>, document);

and the notice goes away.

Screenshot

like image 105
Michelle Tilley Avatar answered Oct 14 '22 14:10

Michelle Tilley