FormidableLabs / use-editable

A small React hook to turn elements into fully renderable & editable content surfaces, like code editors, using contenteditable (and magic)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No parent error.

Aslemammad opened this issue · comments

Hey @kitten✋ , I was working on my codesandbox dropdown(not ready yet), then I realized something. When I use setCode sometimes it gives me this error:
Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
I worked on it for many hours and I found here's the problem:
line: 295, file: useEditable.ts

mutation.target.removeChild(mutation.addedNodes[i]);

some mutation.addedNodes[i] don't have parents(parentNode , ... = null) and they don't know mutation.target as their parent node.
so I convert the ts to js and I edited the above piece like this:

if (mutation.addedNodes[i].parentNode) {
              mutation.target.removeChild(mutation.addedNodes[i]);
 }

and it worked successfully.
in the Edit.js of the codesandbox you can comment the regular import and uncomment the relative import comment.

// import { useEditable } from "use-editable";
import { useEditable } from "./useEditable";

to see the successful result.
Can I make the PR for it?

Hmm, so I generally got this error rarely on some browser testing and haven't been able to reliably trigger it. (Edit Note: Btw this is what I saw on IE11 several times as changes queued up, but IE11 is unreliable and unsupported by this lib anyway)

I'd like to look into the order of the MutationObserver events first, because it's important that it's always doing the right thing.

So if it just did that if statement check, there's a small chance that it wouldn't do the right thing and would mangle the DOM and introduce differences that React's virtual DOM wouldn't expect 😅

Yea, I got it😅, It was just a solution and I was so curious why an if statement fixed that. and I wasn't able to code anymore because of that error.