Note: render is deprecated as of React 18
Let use first look at the signatures of both the methods.
Both the methods takes the element to render and the container where the element should be placed in the dom. Both does the job well.
This got me wondering what is the difference between the two:
- Even though the element is place outside the regular dom tree, in case of createPortal the child still lies within the parents component. The parent of the element rendered using createPortal can handle all the events hapenning on the child. That is, the events are bubbled back to the parent in case of createPortal.
- The other difference that sets createPortal apart from render is access to the same context as the parent. Many libraries like redux rely heavily on context and using the render method will break the child out of the context tree and makes it inaccessible. Where as the same doesn’t happen with createPortal.
In the code above there are two h1 elements, one rendered using render and the other using createPortal. Each of the elements are enclosed within a parent div with a onClick handler.
- In case of createPortal(Toolbar2), when the click happens on the h1 element, as mentioned above the event is bubbled to the parent and triggers the onClick handler and prints Example 2 onto the console.
- In case of render(Toolbar1), the click on the h1 element doesn’t bubble back to the parent div hence nothing is printed onto the console.