react icon indicating copy to clipboard operation
react copied to clipboard

dangerouslySetInnerHTML doesn't properly reconcile

Open vjeux opened this issue 13 years ago • 20 comments

@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?

vjeux avatar Sep 27 '13 03:09 vjeux

One other tidbit: this only seems to happen when the __html value is exactly the same in step 3 as it was in step 1. If it's different (i.e. '<b>html 2</b>'), things work as expected.

brianr avatar Sep 27 '13 03:09 brianr

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

petehunt avatar Sep 27 '13 03:09 petehunt

I just reduced the test case to 19 lines.

vjeux avatar Sep 27 '13 03:09 vjeux

@petehunt: yeah changing 2) to use a span works as expected

vjeux avatar Sep 27 '13 03:09 vjeux

If anyone wants to look into this, the bug is probably here https://github.com/facebook/react/blob/master/src/core/ReactNativeComponent.js#L280

petehunt avatar Sep 27 '13 03:09 petehunt

@brianr: I cannot reproduce what you are seeing: http://jsfiddle.net/vjeux/FwvMt/ It also bugs if I use a different string

vjeux avatar Sep 27 '13 03:09 vjeux

@vjeux I'm not able to reproduce that particular behavior given this small repro case. The original code had several more layers... will keep trying.

brianr avatar Sep 27 '13 03:09 brianr

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.

adewes avatar May 10 '15 18:05 adewes

thx @adewes you saved my day :-)

cdujeu avatar May 15 '15 06:05 cdujeu

Seems like the issue is back again. @adewes Thanks, solves the problem for me too.

LeZuse avatar May 17 '15 21:05 LeZuse

@adewes @cdujeu @LeZuse If any of you can post a simple repro case showing the broken behavior here, I'd be happy to take a look at fixing it.

sophiebits avatar May 17 '15 22:05 sophiebits

Took me a while to repro, but here you go: http://jsfiddle.net/LeZuse/uoff476z/2/ (ugly, but demonstrates the issue well)

LeZuse avatar May 17 '15 23:05 LeZuse

@LeZuse Thanks. This is the same issue as #1232. No fix currently, but using the key as a workaround is fine and I'll see if we can fix this sometime.

sophiebits avatar May 18 '15 23:05 sophiebits

Thanks @adewes that was giving me fits!

sreahard avatar Oct 22 '15 15:10 sreahard

If anyone is still seeing issues on 0.14, please make a new issue with a simple repro case.

sophiebits avatar Oct 22 '15 15:10 sophiebits

I have this issue, but can't reproduce in a simple example - my original code is layered and I think that's the reason the real DOM doesn't reconcile. In the react debugger is looks as dangerouslySetInnerHTML has proper __html value, but it doesn't equeal to the innerHTML of the actual element.

I used an extremely simple (and equally nasty) workaround. Writing it here just in case it will help someone:

componentDidUpdate: function() {
    // force html update
    if (this.props.html !== ReactDOM.findDOMNode(this).innerHTML) {
      ReactDOM.findDOMNode(this).innerHTML = this.props.html;
   } 
} 

ksavenkov avatar Dec 30 '15 13:12 ksavenkov

Reading .innerHTML out will rarely give you what you put in because the browser does various kinds of normalization on it. If you can repro with a standalone example though I'm happy to take a look.

sophiebits avatar Dec 30 '15 16:12 sophiebits

well, that's more like a control shot :-)

ksavenkov avatar Dec 30 '15 22:12 ksavenkov

Looks like the issue still exist in 16.4.1 when using dangerouslySetInnerHTML on a <p> tag, you cannot nest other <p> or <ul>. issue is fixed by replacing the container with a <div> 🤔

revolunet avatar Aug 23 '18 09:08 revolunet

If you experience something similar three years later, and the original issue is closed, you can be sure it's a different unrelated issue. :-) Please file a new one. We don't keep track of closed issues so your feedback is unfortunately going into the void. When filing a new issue, please provide a reproducing example.

when using dangerouslySetInnerHTML on a <p> tag, you cannot nest other <p> or <ul>

This sounds like browsers work. You can't nest <p> into <p>. This is why React warns when you do it (except dangerouslyInnerHTML in which case React can't validate it).

gaearon avatar Aug 23 '18 19:08 gaearon