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.

React DOM tree root should always have a node reference

See original GitHub issue

I’ve built a task execution pipeline utilizing React v14 and I’ve begun testing it against React v15 and have noticed that all of a sudden I’m getting an invariant error: React DOM tree root should always have a node reference.

When I comment the line that is causing this error (https://github.com/facebook/react/blob/3b96650e39ddda5ba49245713ef16dbc52d25e9e/src/renderers/dom/client/ReactDOMComponentTree.js#L171) everything works as it did with v14, and all of my unit tests pass. I assume this check is important to the new internal updates that have been made since v14.

I was able to make it work with a hacky bit of code that adds properties to the instance, _flags and _nativeNode, you can see this here: https://github.com/mbrio/react-pipeline/blob/react-15/src/startTasks.js#L8-L9.

I believe the issue is that with v14 I was able to use the server rendering code but in v15 I can no longer call forceUpdate or setState without errors occurring. Is there a way of bypassing the above error using the client renderer, or a way of calling setState/forceUpdate with the server renderer?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:14
  • Comments:53 (17 by maintainers)

github_iconTop GitHub Comments

31reactions
rafaelbitencommented, Nov 30, 2016

I was having the same problem and after reading a few comments I understood the problem. I have a modal that can be closed by clicking on the dark background overlay or by clicking a close button inside the modal content container. Clicking the overlay was fine, but clicking the button would cause the error since it was also triggering the same event on the overlay.

Adding event.stopPropagation(); to the event handler fixed the issue for me.

13reactions
mnpennercommented, Sep 20, 2016

I’m hitting this error when I call close() in this function:

export default function openReactModal(componentFn, modalProps={}) {
    let reactRoot = document.createElement('div');
    document.body.appendChild(reactRoot);
    const close = () => {
        if(reactRoot === null) {
            console.warn('You shouldn\'t call close() more than once');
            return;
        }

        ReactDOM.unmountComponentAtNode(reactRoot);
        reactRoot.parentNode.removeChild(reactRoot);
        reactRoot = null;
    };
    ReactDOM.render(<Modal isOpen={true} {...modalProps}>{componentFn({close})}</Modal>, reactRoot);
}

Why can’t I unmount a root component?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invariant Violation in React Render OR the proper way to ...
I'm running into persistent problems in my React renders. This code /** @jsx React.DOM */ var AnswerRows = React.
Read more >
How to Access DOM Nodes in React with Refs - Fullstack.io
We access a DOM element using the current property of a reference created with the React.createRef() function. React will update this property ...
Read more >
ReactDOM – React
Portals provide a way to render children into a DOM node that exists outside the hierarchy ... If you need a reference to...
Read more >
Reconciliation - React
When building up a new tree, new DOM nodes are inserted into the DOM. Component instances receive UNSAFE_componentWillMount() and then componentDidMount() . Any ......
Read more >
ReactDOM.render and the Top Level React API
If you have multiple React roots, or a single root that gets deleted over time, we recommend that you always create your own...
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