react-rails icon indicating copy to clipboard operation
react-rails copied to clipboard

Not HTML escaping on server render

Open hongkim91 opened this issue 11 years ago • 5 comments

When I attempt to render a string literal that has characters that need to be HTML escaped, I get warnings like this. It seems to be the case that the escaping did not happen on the server side while it did on the client. Why would this be happening?

Warning: React attempted to reuse markup in a container but the checksum was invalid. (...) (client) 0.3.0.0.1.1.0.0.0">I&#x27;m</div> (server) 0.3.0.0.1.1.0.0.0">I'm</div>

The smallest example of a component I am trying to render would be the following: `return (

I'm
);`

hongkim91 avatar Jun 26 '15 21:06 hongkim91

Thanks for reporting & including a case to reproduce it. In short, I could not reproduce this issue. I gave a try with:

  • Render function

      render() {
        return <div>I'm an apostrophe & + "</div>
      },
    
  • and into the Rails view:

      = react_component("Mtg.App", {}, prerender: true)
    
  • In view-source, I could see it was prerendered:

          <div data-react-class="Mtg.App" data-react-props="{}"><div data-reactid=".ctiakk93wg" data-react-checksum="-1223354208">I&#x27;m an apostrophe &amp; + &quot;</div></div>
    
  • In a normal browser window, it remounted with no errors:

    image

  • I'm using:

    • React: "0.13.1"
    • react-rails: 1.0.0

Could you share some more info? Maybe your React version, react-rails version, component source, and view mount code

rmosolgo avatar Jun 29 '15 15:06 rmosolgo

@rmosolgo Thanks for looking into this. So this problem disappeared for me during development, and after your comment I dug into what had changed, and it seems the problem appeared due to my use of Math.random() to create a dynamic number of dummy comments below the section I was talking about. Obviously using random() is problematic if you want server-client to match, but it's odd that html escaping behavior of different mockup if altered.

This component source reproduces the issue for me.

var Test = React.createClass({
  render: function() {
    var i = 0;
    var arr = [];
    for (i=0; i<Math.random()*10; i++) {
      arr.push(<div>test</div>);
    }
    return (
      <div>
        <div>{"I'm an &"}</div>
        {arr}
      </div>
    );
  }
});

Browser console: screenshot 2015-06-29 12 46 21

I'm using:

  • React: 0.13.3
  • react-rails: 1.0.0

hongkim91 avatar Jun 29 '15 19:06 hongkim91

Thanks for the details, yes, I'm able to reproduce it with Math.random. However if I push a defined number of <div>s, I don't get any error.

Do you get an error if you push a fixed number of divs?

If this only occurs with Math.random, I'm inclined to leave it as-is. The rendered output looks good & React successfully remounts the components. Perhaps the difference is how React warnings format text from different sources.

rmosolgo avatar Jun 29 '15 20:06 rmosolgo

No I don't get an error for a fixed amount of divs. I'm fine with closing this issue, since it does work (as you mentioned) & you shouldn't be rendering different markup on server and client anyways. It was frustrating for me at the moment though. Thanks for the replies!

hongkim91 avatar Jun 29 '15 21:06 hongkim91

:sparkles: :+1:

rmosolgo avatar Jun 29 '15 21:06 rmosolgo