openseadragon: Failed to execute 'removeChild' on 'Node' React issue after 5.0 update due to innerHTML being wrapped

There is an issue now with React and using ReactDOM.createPortal(children, rootElemRef.current) where rootElemRef is an actual reference in addOverlay and the children are whatever you would put inside an overlay- (basic idea for wrapper taken from here)

viewer.addOverlay({
        element: rootElemRef.current,
        ...props
      })

When removed the React throws Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. This is due to the fact that innerHML now gets wrapped in div, link to commit

This could be bypassed by using rootElemRef.current.children[0] but then again it’s not an elegant solution as it is better to manipulate the actual ref as well as having an issue of detecting when the child is actually there since addOverlay isn’t triggering a rerender.

I know React isn’t the scope of this project but still mentioning this incase someone has the same issue or has a better solution or an idea.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

I can get the demos to look the same, but the unit tests are failing. So still working through the fix.

We could do any of the following:

  1. Tear out the flipping of the content.
  2. Find a better way of wrapping.
  3. Add some additional tests for some of the well known packages that depend on OSD to see if changes break stuff such as this React package.

I’d vote for option 2.

I think what @stephenwf proposed above makes sense. I would guess the root of the problem is that the wrapping is done by setting this.element.innerHTML = "<div>" + this.element.innerHTML + "</div>"; - the problem is, all references to the original DOM elements (and bound data, event handlers, etc) are lost since they are re-created from new HTML text. Appending the existing DOM element to the new wrapper via wrapper.appendChild(existingNode) will help a lot. And it would be ideal to set all of the properties (like style) on the wrapper so that OSD is only changing elements that it controls.

Hmm, yes the setting of the innerHTML seems to be causing the issue there.

I think if the overlay needs a wrapper there could be 2 elements on the Overlay class.

So this:

this.element.innerHTML = "<div>" + this.element.innerHTML + "</div>";

Could be:

this.wrapperElement = document.createElement('div');
this.wrapperElement.appendChild(this.element);

And then you could apply the CSS transforms to the outer element. It would be essentially the same thing, but the contents of the passed in element are unchanged.

Although it is React that is throwing this error, I think it could be replicated:

const overlay = document.createElement('div');
const something = document.createElement('p');

overlay.appendChild(something);

// Pass `overlay` to the Overlay class in OSD.

// Later...
overlay.removeChild(something); // throws

something.innerHTML = '...'; // Does not apply the changes

If the overlay is passed in explicitly, I feel like the DOM elements should remain stable.