createPortal is a better option for achieving append-only behavior in React as it allows you to append a child node to a DOM node that exists outside the current React component hierarchy. This means that even if you pass the control to a higher-level component, the child node appended using createPortal will not be unmounted or removed. In contrast, appendChild adds a child node to an existing DOM node within the same hierarchy, and if the parent node is removed, the child nodes will be removed as well.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-11 23:20:11 +0000
Seen: 14 times
Last updated: Jun 11 '23