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.

dangerouslySetInnerHTML doesn't properly reconcile

See original GitHub issue

@sbezboro reported the following issue on IRC:

http://jsfiddle.net/vjeux/8vZ57/1/ (Both on master and 4.0)

The repro case is:

  1. Render <div dangerouslySetInnerHTML={{__html: '<b>HTML</b>'}} />
  2. Render <div>Text</div>
  3. Render the same as 1)

The div has its innerHTML empty instead of <b>HTML</b>.

I don’t really know what’s going on. @jordwalke, @petehunt can you guys look at it?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

13reactions
adewescommented, May 10, 2015

I know this is a very old issue but I had a similar problem and came across this.

I found that this problem occurs when you do not add a key property to the React component containing the dangerouslySetInnerHTML property. So,

<div dangerouslySetInnerHTML={{__html : markdownText}} />

does not seem to work reproducibly, whereas

<div key="some_unique_key" dangerouslySetInnerHTML={{__html : markdownText}} />

seems to. I think this could be related to React’s diffing algorithm that might not properly treat a component with a dangerouslySetInnerHTML statement if that component gets rerendered multiple times with different inner content. Just posting this here in case other people have the same issue and stumble upon this.

1reaction
petehuntcommented, Sep 27, 2013

What happens if you change step 2 to a span instead of a div? It works, right?

Read more comments on GitHub >

github_iconTop Results From Across the Web

React.js: Set innerHTML vs dangerouslySetInnerHTML
Assume I'm properly sanitizing things for the sake of simplicity. Example: var test = React.createClass({ render: function(){ ...
Read more >
Using dangerouslySetInnerHTML in a React application
dangerouslySetInnerHTML is a property that you can use on HTML elements in a React application to programmatically set their content. Instead of ...
Read more >
Design Principles - React
For example, dangerouslySetInnerHTML is hard to miss in a code review. Optimizing for search is also important because of our reliance on codemods...
Read more >
fx-team - Mercurial - Mozilla
This build has "addons", or + * functionality we've built and think might be useful but doesn't have a good + * place...
Read more >
Dangerously Set innerHTML | React
The prop name dangerouslySetInnerHTML is intentionally chosen to be frightening, ... After fully understanding the security ramifications and properly ...
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