question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warning "Expected server HTML to contain a matching <div> in <div>"

See original GitHub issue

Well I am creating a component with <%= react_component(‘NameOfComponent’, {}, prerender: false) %>

and React is logging into the console Warning “Expected server HTML to contain a matching div in div

For what I seen it does not log it if the component is creating using render instead of hydrate

My question is how can I stop the warning.

I read https://github.com/reactjs/react-rails/pull/828 and then in your code

        if (typeof ReactDOM.hydrate === "function") {
          ReactDOM.hydrate(React.createElement(constructor, props), node);
        } else {
          ReactDOM.render(React.createElement(constructor, props), node);
        }

and I thought, maybe you are forcing the hydrate function.

I did force the same method but without using hydrate and the warning goes away.

System configuration

**Webpacker version 3.0.2: **React-Rails version 2.4.1: **Rails version 5.1.4: **Ruby version2.4.2:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jsbaltodanocommented, Nov 27, 2017

Well, it is an issue within the gem but the idea to hack it for a quick check is

Create the file component_mount.rb inside config\initializers

Write the same as the original file but you could add data[:hydrate] = 't' if prerender_options

below data[:react_props] = (props.is_a?(String) ? props : props.to_json)

This would work as a data param for JS to check if ReactDOM.hydrate() must be use or to use the ReactDOM.render() method.

Now, append this to your \app\javascript\packs\application.js

import React from 'react';
import ReactDOM from 'react-dom';

ReactRailsUJS.mountComponents = function(searchSelector) {
    var ujs = ReactRailsUJS;
    ujs.RENDER_ATTR = 'data-hydrate';
    var nodes = ujs.findDOMNodes(searchSelector);

    for (var i = 0; i < nodes.length; ++i) {
      var node = nodes[i];
      var className = node.getAttribute(ujs.CLASS_NAME_ATTR);
      var constructor = ujs.getConstructor(className);
      var propsJson = node.getAttribute(ujs.PROPS_ATTR);
      var props = propsJson && JSON.parse(propsJson);
      var hydrate = node.getAttribute(ujs.RENDER_ATTR);

      if (!constructor) {
        var message = "Cannot find component: '" + className + "'"
        if (console && console.log) {
          console.log("%c[react-rails] %c" + message + " for element", "font-weight: bold", "", node)
        }
        throw new Error(message + ". Make sure your component is available to render.")
      } else {
      	// ReactDOM.render(React.createElement(constructor, props), node);
        if (hydrate && typeof ReactDOM.hydrate === "function") {
          ReactDOM.hydrate(React.createElement(constructor, props), node);
        } else {
          ReactDOM.render(React.createElement(constructor, props), node);
        }
      }
    }
  }

Reset your server.

0reactions
Toske94commented, Sep 13, 2018

I’m new in this, can someone explain me why I got this error

The script that I use is returning a picture, but sometimes the picture is not showing and I got this error:

Expected server HTML to contain a matching in .

import React from 'react';
import {Menu} from 'semantic-ui-react';
import { Link } from '../routes';

export default (props) => {
    return (
      <Menu borderless id="logo">
        <script 
            type="text/javascript" 
            src="https://example.com/link/script/" >
        </script>
    </Menu>
    );
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Warning: Expected server HTML to contain a matching <div ...
The issue is on the client when checking the browser width on component mount, and then setting the state of a component to...
Read more >
Warning: Expected server HTML to contain a ... - GitHub
Hi, I have a navbar , and it displays a different UI based on if a user is logged in or not. I...
Read more >
[Solved]-Next.JS: Warning Expected server HTML to contain a ...
Most of the time, it is due to the missing closing tag/brackets. On nextJS, if you are using <style jsx> then it is...
Read more >
React Pre-rendering and Potential Hydration Issue
Warning : Expected server HTML to contain a matching <div> in <a>. ... TLDR; Hydration makes the pre-rendered HTML interactive in the client....
Read more >
Cory House on Twitter: "A surprising benefit of server ...
This error confused me at first: "Expected server HTML to contain a matching <div> in <p>." This occurred because I had a div...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found