Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hydrating a component with dangerouslySetInnerHTML and toString causes a warning, and the component to not render #11789

Closed
magicmark opened this issue Dec 7, 2017 · 8 comments · Fixed by #13353

Comments

@magicmark
Copy link

Do you want to request a feature or report a bug?

bug

What is the current behavior?

When hydrating a pre-rendered component that uses dangerouslySetInnerHTML, + an object with a toString method, we get a warning such as:

Warning: Did not expect server HTML to contain the text node "Bonjour" in <p>.

The component will also appear blank.

This is noteworthy since:

  • The markup returned by render appears to be correct, meaning:
    • The markup that an SSR server returns is correct
    • The markup that appears on page if we just use render on the client, without SSR is correct
  • It's only if we do both, that we get errors.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem via https://jsfiddle.net or similar (template for React 16: https://jsfiddle.net/Luktwrdm/, template for React 15: https://jsfiddle.net/hmbg7e9w/).

https://codesandbox.io/s/vqkq34o965

(Code, for posterity:)

import React from "react";
import { hydrate } from "react-dom";

const HelloObj = {
  toString: () => "Bonjour"
};

const MyComponent = () => (
  <p
    dangerouslySetInnerHTML={{
      __html: HelloObj
    }}
  />
);

hydrate(<MyComponent />, document.getElementById("app"));

What is the expected behavior?

The component is not blank, and no error is outputted in the console

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

  • react/react-dom 16.2.0
    • This worked < 16 (confirmed working in 15.6.1, if we use the render method)
  • Chrome Version 62.0.3202.94
  • OS X 10.12.6
magicmark added a commit to magicmark/react that referenced this issue Dec 7, 2017
magicmark added a commit to magicmark/react that referenced this issue Dec 7, 2017
magicmark added a commit to magicmark/react that referenced this issue Dec 7, 2017
@nubunto
Copy link

nubunto commented Dec 7, 2017

I would really like to take this! I'm going to take a look more deeply in a few hours/tomorrow. If anyone care to point me to anything, that would be great. Should be my first contribution!

@gaearon
Copy link
Collaborator

gaearon commented Dec 8, 2017

@nubunto Good luck!

@robgietema
Copy link

@magicmark does your last commit fix the issue? (since the tests seem to pass now) And if so can we make a PR for this?

@magicmark
Copy link
Author

@robgietema the PR is here #11792

@tirthbodawala
Copy link

@gaearon @magicmark do we have any update on the issue and PR?

@gaearon
Copy link
Collaborator

gaearon commented Jun 20, 2018

Reviewed the PR: #11792 (review)

@ata-marzban
Copy link

@magicmark @gaearon I found a way to work around this issue and avoid using dangerouslySetInnerHtml using an HTML parser package such as react-html-parser, it basically parses the HTML and outputs react elements so you are not using dangerouslySetInnerHtml at all!

@gaearon
Copy link
Collaborator

gaearon commented Aug 9, 2018

#13353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants