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.

Bug: hydrating fails with nested paragraph and dangerouslySetInnerHTML

See original GitHub issue

React version: 16.13.1

Steps To Reproduce

  1. Use the dangerouslySetInnerHTML on a p element where the __html contains a <p> tag
  2. Server-side render this component and try to hydrate

Link to code example:

<!doctype html>
<html>
  <head>
    <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
    <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  </head>
  <body>
    <!-- Pretend for an instance that this html was generated on the server by ReactDOM.renderToString() -->
    <div id="app"><p><p>Dope!</p></p></div>
  </body>
  <script>
    function Component() {
      return React.createElement("p", {
        dangerouslySetInnerHTML: { __html: "<p>Dope!</p>" },
      })
    }
    const component = React.createElement(Component)
    const root = document.getElementById("app")
    ReactDOM.hydrate(component, root)
  </script>
</html>

JSFiddles:

The current behaviour

The ReactDOM.hydrate throws a cryptic error:

Warning: Prop `dangerouslySetInnerHTML` did not match. Server: "" Client: "<p>Dope!</p>"

Note the missing Server: "" string.

The expected behavior

Do not throw an error and just hydrate the component as expected.

I realise that nesting p elements does not conform to the HTML spec for the p element. If React chooses not to support the use case of nesting p tags through dangerouslySetInnerHTML, that’s understandable. In that case the error should be more descriptive and there should probably also be an error or warning while performing the server side render. It should also be documenting somewhere that will easily show up in Google.

The current behaviour is very confusing and it took me a while to track down what was going on.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
matthewdiamantcommented, Jan 9, 2021

Your issue may be because your __html top most element is a <p> tag, and you are using dangerouslySetInnerHTML on a <p> tag.

I also ran into this issue, and found that using a outermost <span> tag fixes the issue.

0reactions
OmarZeidancommented, Nov 18, 2022

I have faced the same issue when I am using dangerouslySetInnerHTML on a <p> tag.

The fix was to use it on a <div> instead.

In my case, I am using ChakraUI and the dangerouslySetInnerHTML was on a <Text dangerouslySetInnerHTML ...>; I replaced the <Text> with <Box dangerouslySetInnerHTML ...> the issue disappeared!

The reason, is hydration won’t be possible if there is a <div> inside a <p> and since your content coming for an outside source, it might contain a <div> and that is not a valid HTML 😃

Hope that helps @romeovs @Limnades

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve react hydration error in Nextjs
I have created small nextjs page using wordpress REST API, Now react-hydration-error error show this page.I am using react html parser npm.
Read more >
How to fix the `dangerouslySetInnerHTML` did not match ...
The string I was trying to print appeared for a while, and then disappeared. Quite strange! The error message is cryptic but after...
Read more >
@mui/system | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
DOM Elements
If you use React with Web Components (which is uncommon), use the class attribute instead. dangerouslySetInnerHTML. dangerouslySetInnerHTML is React's ...
Read more >
Beginning ReactJS Foundations Building User Interfaces ...
getDerivedStateFromErrors Receives the Error as a Parameter ... This code looks for a paragraph with the id attribute equal to para1 and changes...
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