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)
I can get the demos to look the same, but the unit tests are failing. So still working through the fix.
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 viawrapper.appendChild(existingNode)will help a lot. And it would be ideal to set all of the properties (likestyle) 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
Overlayclass.So this:
Could be:
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:
If the overlay is passed in explicitly, I feel like the DOM elements should remain stable.