If state is true to play youtube video, and it is false I would like to delete youtube playing. MY code is as follows.
{this.state.isPreViewVideo && <PlayYouTube video_id="ScSn235gQx0" />}sandbox URL:
Reproduction method:
If 4-digit characters are included in input form, "isPreViewVideo: true" by setState and if it is less than false
It works fine when state is true, but when state is false, I encounter this error as follows.
DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.is there a way to avoid or resolve this error?
14 Answers
In playYouTube.tsx line 78 replace <React.Fragment>...</React.Fragment>
with <div>...</div>
Fragments let you group a list of children without adding extra nodes to the DOM.
This explains the error Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
More on fragments here
8This error, Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node, can also be raised if Google Translate is used (or any other plugin that changes the page DOM).
This is detailed in this Github issue:
0Problem explanation:
When isPreViewVideo is truthy, you have this:
<PlayYouTube video_id="ScSn235gQx0" />That probably renders to something like this:
<div> <!-- the root element as rendered by PlayYouTube if isPreViewVideo is truthy --> <embed /> <!-- the video player or whatever PlayYouTube renders -->
</div>Now if some code removes the player by directly manipulating the DOM and removing the root div, you'll end up with... Well, with nothing.
But in React's virtual DOM, the root div still exists! So when isPreViewVideo goes falsy, React tries to remove it from the real DOM but since it's already gone, the error is thrown.
The solution:
To expand on @henrik123's answer - wrapping PlayYouTube with a div like this...
<div> <!-- the root element rendered if isPreViewVideo is truthy --> <PlayYouTube video_id="ScSn235gQx0" />
</div>...causes this to render:
<div> <!-- the root element rendered if isPreViewVideo is truthy --> <div> <!-- the element as rendered by PlayYouTube --> <embed /> <!-- the video player or whatever PlayYouTube renders --> </div>
</div>Now the same code that removes the player by removing its root div makes it look like this:
<div> <!-- the root element rendered if isPreViewVideo is truthy -->
</div>Now when isPreViewVideo goes falsy, the root exists both in React's virtual DOM and in the real DOM so there is no problem in removing it. It's children have changed but React doesn't care in this case - it just needs to remove the root.
Note:
Other HTML elements but div may work too.
Note 2:
Wrapping with React.Fragment instead of a div would not work because React.Fragment doesn't add anything to the real DOM. So with this...
<React.Fragment> <PlayYouTube video_id="ScSn235gQx0" />
</React.Fragment>...you still end up with this:
<div> <!-- the root element as rendered by PlayYouTube if isPreViewVideo is truthy --> <embed /> <!-- the video player or whatever PlayYouTube renders -->
</div>And you have the same issue.
TL;DR solution:
Wrap PlayYouTube with a div.
SOLVED for bootstrap alert after manually close button
I had exact error when i close the bootstrap alert error message if it's there manually and then submit the form again. what i had done is to wrap up the AlertError component with extra tag around.
import React from "react";
const AlertError = (props) => { return ( <div> // <---- Added this <div className="alert alert-custom alert-notice alert-light-danger fade show"> <div className="alert-icon"><i className="flaticon-warning"></i></div> <div className="alert-text">There is an error in the form..!</div> <div className="alert-close"> <button type="button" className="close" aria-label="Close"> <span aria-hidden="true"><i className="ki ki-close"></i></span> </button> </div> </div> </div> // <---- Added this );
};
export default AlertError; 1 This error can occur when you use external JS or jQuery plugins. Let me tell about my case.
Issue description
In my React project I have some links on a page, every link opens a modal with some info and a slider with images inside it. Every link has its own set of images inside the slider. But I have the single modal, it is the same for every link. Data for modal is stored in a state variable and I change its content on every link click (on every modal open):
const initialData = { title: "", intro: "" }; // here is some empty structure of object properties, we need it for the modalResponse initialization
const [modalData, setModalData] = useState(initialData);
// ...
<div> <div className="title">{modalData.title}</div> <div className="intro">{modalData.intro}</div> <div>{modalData.sliderHtml}</div>
</div>When I open modal using setModalData() I fill modalData with some new data. HTML-code inside modalData.sliderHtml is something like this structure:
<div> <div><img src="first.jpg" /></div> <div><img src="second.jpg" /></div> <div><img src="third.jpg" /></div>
</div>When modal has opened I call some jQuery code for slider initialization:
useEffect(() => { $('.carousel').initCarousel({ // some options });
}, [modalData]);Then user could close the modal and click the next link. The slider must be filled by new set of images. But I get the error: Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Error reason
The reason of the issue is in a fact that the jQuery plugin changes the structure of html during initialization. It creates some new blocks inside the .carousel parent (arrows to the left/right, dots for navigation, etc.). And it moves all the .item children inside the new .items block! I get such html structure after the plugin initialization:
<div> <div>...</div> <div>...</div> <div> <div><img src="first.jpg" /></div> <div><img src="second.jpg" /></div> <div><img src="third.jpg" /></div> </div>
</div>When React tries to change the content of modalData.sliderHtml it executes some magic DOM operations for old structure removing. One of this operations is removeChild method, but the old .item elements can't be found inside the parent .carousel because the plugin moved them inside the new .items element. So, we get the error.
Bug fixing
I started to change modalData content back to the initialData structure every time when the modal is closed.
useEffect(() => { $('#MyModal').on('hidden.bs.modal', function (e) { setModalData(initialData); })
}, []);So, all html structure inside modalData.sliderHtml is filled by the initial empty data again and we don't get an error on the new link click.
Most probable cause: something modified the DOM after React
The most probable cause of this issue is that something other than React modified the DOM after React rendered to it.
What's causing this DOM mutation?
Depending on the profile of users who are impacted, there might be a few different causes.
Everyone
If all your users are impacted, you, the developer might be the culprit.
The cause is probably that you are using jQuery or doing DOM manipulations outside of React.
Usual users
If usual users are affected, the most probable cause is Google Chrome translate feature (or Microsoft translate on Edge Chromium).
Thankfully there's an easy solution for that: add a <meta> tag in your HTML <head> (Google documentation).
<meta name="google" content="notranslate" />This should also disable Microsoft Translator on Edge Chromium.
Advanced users
If only advanced users are affected, and they are not using a translation feature on their browser, then it might be a browser extension or some userscript (most likely run by SpiderMonkey) that causes the problem.
More info
- Issue on React repo
- SolidJS is also affected
- Issue on Chromium bug tracker (please star it to help prioritize it)
I had same problems with a audio tag and I kept this tag within two nested div and now its working
0This is a very annoying bug to fix. But you should wrap the component that you are rendering it conditionally with span element.
Check this GitHub issue:
In one place of our code was something like this:
render() { if (el) return <div /> return '';
}After replacing ' ' with null, all works without errors.
I had the same issue with react. The problem was caused by a google-auth script tag in index.html.
<script src="" async defer></script>
This is while I had another on the JS file causing the issue. Be sure to check for conflicting or repeated CDNs.
In my case, I just needed to reset the state OR make it to initial data when I needed to render it with new data. It fixed the issue.
Ex: This issue occurred when I was setting table data upon getting API response in useFetch. Before setting the data to state, I first set it to initial value. setTableData([]); setTableData(data.response);
I hope it may help someone.
I meet this error too,because I use React.createPortal and want to override the contents of the container. like this:
const node = document.getElementById('middle-bottom-panel'); useMemo(() => { // eslint-disable-next-line no-unmodified-loop-condition while (node && node.firstChild) { node.removeChild(node.firstChild); } }, []);At last I give up removeChild, and use ReactDOM.render, thing seems become all right.
ReactDOM.render(<div/>, node); In my case the issue occurred when using React.forwardRef. My code snippet looked like this:
const App = () => { const ref = React.useRef(null); const VideoPlayer = React.forwardRef(({source}, ref) => { return ( <video ref={ref} id='player' src={source}/> ) }) return ( <VideoPlayer source=' ref={ref}/> )
)}Every time React state changed, and VideoPlayer re-rendered, it would throw the following error:
Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Solution
All I had to was wrap my <video> tag in a div, like this:
const VideoPlayer = React.forwardRef(({source}, ref) => { return ( <div> <video ref={ref} id='player' src={source}/> </div> )
})As others have mentioned, using React.Fragement or <> would not work, as it added nothing to the DOM.
Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
fix:
unmountContainer() { // macro task setTimeout(() => { // refs if (containerEle) { // receive removed node // eslint-disable-next-line no-unused-vars let removedChild = document .querySelector('.page__wrapper') .removeChild(containerEle); } }); }