Ask Your Question
3

Which option is better for achieving append-only behavior between createPortal and appendChild in React?

asked 2023-06-11 23:20:11 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-11 23:32:01 +0000

lalupa gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-06-11 23:20:11 +0000

Seen: 14 times

Last updated: Jun 11 '23