react icon indicating copy to clipboard operation
react copied to clipboard

React 15.0.1 Updates causing issues with DOM

Open jbbae opened this issue 10 years ago • 61 comments

Hey, So I'm a fairly new developer who had a working app until version v0.14.8, and just updated to v15.0.1 today. The update did remove a few errors/warnings I was struggling with (so a great relief there), but brought up this issue.

Specifically, the app itself renders fine, with all the pages looking fine (all components, text, etc. in the right places), but is just "frozen" - completely non-functional, not allowing me to click on anything etc.

The error message I see are as follows (from Chrome and Firefox): capture capture2

I back-tracked it to where I could and found this being the part where the "node" is coming off as null: capture3

So I have absolutely no clue what's going on and there seems to be no hint as to what exactly is causing this issue. I will try ripping the app apart and do a thorough debugging, but wouldn't expect much from it (due to my unfamiliarity with development yet...). I really really appreciate the help in advance, and hope that this problem can contribute somewhat to improving the React ecosystem!

jbbae avatar Apr 10 '16 07:04 jbbae

React 0.14 warns about deprecations that are removed/changed in React 15. I would suggest to resolve all the errors/warning in 0.14 and then upgrade.

roganov avatar Apr 10 '16 08:04 roganov

Please make sure you don’t have two instances of React on the same page. npm ls react can help to determine this.

If you don’t, please share the minimal example reproducing this issue on GitHub or JSFiddle. Thanks!

gaearon avatar Apr 10 '16 11:04 gaearon

I am also seeing this error, particularly in unit tests that do not unmount components after the tests complete. In these cases, the value of rootNode.parentNode is null, presumably because the node is no longer attached to the DOM. The null value is then passed into getClosestInstanceFromNode, where the error is thrown due to node being null.

My current workaround is to return null if the node is null:

if (node == null) {
    return null;
}

Which seems to work, though I admittedly don't know the full consequences for doing so. I think this is at least a candidate for an invariant warning/error.

doctyper avatar Apr 13 '16 15:04 doctyper

I should add, this error surfaces after testing with ReactTestUtils.renderIntoDocument, which:

  1. does not have a way to unmount a test component
  2. does not attach the test component to the document

I believe this would explain why parentNode would be null.

It also returns null if the node has just been created and is not yet attached to the tree.

doctyper avatar Apr 13 '16 16:04 doctyper

@nickbae91 @doctyper I am still unable to reproduce. Here is my fiddle: http://jsfiddle.net/jtxx6goy/ Can one of you provide a jsfiddle that demonstrates the issue?

jimfb avatar Apr 13 '16 17:04 jimfb

Browser

Chrome v49

OS

OS X 10.11.4

Fiddle

https://jsfiddle.net/m1hepcr3/

Summary

I actually debugged this very issue yesterday morning while trying to upgrade to v15. The solution I came up with was to provide ReactDOM with an element that already existed in the DOM via document.getElementById.

One of the projects I work on has an old backbone codebase and it has slowly been migrated to react. The element that ReactDOM was being told to render to was a div that backbone generated via document.createElement. Previously (before I upgraded to v15) this element was never being inserted into the DOM.

My stack trace revealed that getClosestInstanceFromNode had a null node as @doctyper says above. Debugging this further I discovered that ReactEventListener.js was the one calling getClosestInstanceFromNode. If you take a look at findParent you can see where the node gets defined. In my case the rootNode was an audio element that a react class was producing. The audio element had no parentNode, and so container became null.

While debugging this issue occasionally it would just disappear, and stop reporting. I would refresh a dozen times and see the error once again.

Steps to Reproduce
  1. Open Fiddle
  2. Open Console
  3. Press Run
  4. Observe mounting... logs and errors

I couldn't get it to reproduce at first. So I started spamming the run button and eventually saw the error. For the sake of everyones sanity I added a for loop that runs the code 1000 times so everyone can easily see that this does indeed reproduce the issue.

Example

EDIT 1

One thing I find interesting is if you edit the fiddle so that the audio element has no source elements, the errors do not appear. https://jsfiddle.net/g7esummj/

vinnymac avatar Apr 14 '16 05:04 vinnymac

I have a (relatively) consistent repro in chrome: https://jsfiddle.net/r4ewebqs/

Still unable to reproduce in Firefox.

Almost looks like it might be a chrome race condition, potentially related to garbage collection, because the hit rates go way down if you spread out the calls to ReactDOM.render temporally. But the OP suggests that they've seen the failure in firefox also.

jimfb avatar Apr 14 '16 06:04 jimfb

@jimfb The OP has a screenshot of the error appearing in Firefox. I attempted your fiddle as well as my own in Safari 9.1 and Firefox 45, and did not get any errors.

@nickbae91 could you provide additional information, such as the version of the browsers you tested with?

vinnymac avatar Apr 14 '16 06:04 vinnymac

Yeah, if I upgrade from Chrome 49 (out-of-date) to Chrome 50 (latest stable), the error goes away. This is consistent with my original observation that it appeared to be a race condition potentially related to garbage collection.

I'm going to close this out as a browser bug, unless someone is able to reproduce on Chrome 50. I'd also be curious to see a firefox repro.

jimfb avatar Apr 14 '16 17:04 jimfb

Let's be careful about assuming a GC issue / browser bug. We should be able to confidently say what specifically is at play. It could well be a bug in React (particularly around transactions, mounting, and local events)

My best guess here (especially since all the reports are for nodes that we have to add load / error listeners to directly) is that timing is at play. With the switch to createElement we'll actually kick off the fetching of resources earlier than before - instead of waiting for the browser to parse the img element and see src, we create the img and set src right there. That means we'll probably get a load event earlier. How early and is it possible for that event to fire before we actually attach a listener to that node is the question, and what impact does it actually have. Does it only happen on updates? Are there other events actually at play?

One thing I find interesting is if you edit the fiddle so that the audio element has no source elements, the errors do not appear

This would support my theory here as you won't get at least some of the media events.

zpao avatar Apr 14 '16 18:04 zpao

@jimfb I can confirm. Upgraded to Chrome 50 and I am no longer able to reproduce the issue. Hoping we can get some other reproductions of this from the OP. @nickbae91 was the only one to report seeing this elsewhere. In the meantime I'll think about a way to support @zpao's theory.

EDIT 1

@zpao I saw this error once again using Chrome v50. I was on a train and my battery was dying so I didn't have time to take a screenshot of it. I will see if I can reproduce it again, although this lends to the idea that it is timing related. I was not using our fiddles, but instead one of the closed source projects I work on. It occurred during initial page load.

EDIT 2

I finally saw the error again during startup. I have yet to debug it, but here is some proof. Example of Error in Chrome 50

vinnymac avatar Apr 14 '16 23:04 vinnymac

I also bumped into something similar with unit tests running in PhantomJS 2.1.1 after upgrading to React 15.1.0. Worked fine in 0.14.8:

undefined is not an object (evaluating 'node[internalInstanceKey]')
getClosestInstanceFromNode@...
getInstanceFromNode@...

jmullo avatar May 25 '16 07:05 jmullo

Please try to provide a minimal example reproducing this.

gaearon avatar May 25 '16 11:05 gaearon

Seems to be some kind of timing issue, as the cases pass if I delay calls to ReactTestUtils.Simulate.focus() or ReactTestUtils.Simulate.click().

jmullo avatar May 26 '16 13:05 jmullo

I also saw this error when rendering img. But when I remove the "src" attribute in img, this error disappeared.
image

This error only exist when using npm package with webpack. Everything is OK with individual build.

harryhaibojiang avatar Jun 02 '16 04:06 harryhaibojiang

I can confirm that several of our mobile users on Android, using Chrome 34.0.1847, are getting this error. We are on React 15.1.0 and webpack.

Stacktrace can be found on: https://my.trackjs.com/shared/NWEwNzhlMjQ0ZGZlNDFlYmJmNjAwYWJkNTVkMWIyMzc

torarnek avatar Jun 23 '16 11:06 torarnek

@torarnek There is very little we can do without a repro. If you can share the code where this is occurring, we can dig deeper. Otherwise, this is inactionable.

haibo3318 mentioned that things appeared to work with the individual builds / bower builds. Do you find the same to be true?

jimfb avatar Jun 23 '16 14:06 jimfb

I'm facing the same issue, albeit for me it is occurring due to a video tag. screen shot 2016-06-27 at 10 12 59 pm

gauravChaturvedi avatar Jun 27 '16 20:06 gauravChaturvedi

I was able to solve this by doing the following:

Instead of declaring my parent HTML Node inside index.html as :-

    <div id="root"></div>
    <script src="./bundle.js" type="text/javascript"></script>
</body>

I removed the div tag from index.html and added the div from my js code as follows :-

const root = document.createElement('div');
document.body.appendChild(root);
ReactDOM.render(<App />, root);

Hope this works for others as well !

gauravChaturvedi avatar Jun 27 '16 21:06 gauravChaturvedi

@gauravChaturvedi Can you share a minimal project that reproduces the problem?

gaearon avatar Jun 27 '16 23:06 gaearon