React portal in versions prior to React 16

React 16 was out sometime back with cool new features like fragments, portals, custom DOM attributes and more. Portals no longer have to be called with this unstable_renderSubtreeIntoContainer, instead its just createPortal now.

This blog will cover on how one can harness the power of portals in older versions of React which had portal as an unstable feature, as I did not find many examples on how to do so.

Till now React only allowed us mount children inside a parent and not anywhere else which made us design the app in a different way, but not anymore as portals allows to render a child outside of its parent and still keep it bound to its parent state. Cool right!

Lets begin with this simple example of Image Gallery which needs a modal to be rendered just inside body tag. You can stop at any point and look at the code here and you can check the demo here. I have used react-webpack-boilerplate to quickly start off with the app.

Clone the repository and just do npm install to install the dependencies. I am using React 15.6.2.

Component structure of our app:

In this app whenever a user clicks on an image we would like to show a modal with that image, which is where portal comes in handy. In react 15 to implement a portal we need to call unstable_renderSubtreeIntoContainer method of ReactDOM. Do not worry its not at all unstable infact Facebook devs were using it too prior to the release of React 16.

Lets write our App component:

The app looks something like this now:

Here we pass the image list to Gallery and render the images. Next we define Gallery component where all the magic happens:

So here we define a modalClickHandler which will be passed to Image component. Now whenever a user clicks on the image, src and title will be updated.

Before adding a portal, lets just see how the portal works. We need to pass three parameters into the method unstable_renderSubtreeIntoContainer.

  1. context i.e. this
  2. component which should be ported
  3. node to which it should be appended

Let’s first define the node to which it should be appended:

Now, whenever Gallery is rendered, we want imagePreviewContainer to be appended to body . Lets do that when the component mounts:

Lets define our Modal component which will take src, title and show a cool modal with an overlay.

We will add a hook to modalClickHandler, so that whenever state is updated with image src and title we open the modal. Let’s update our modalClickHandler code:

Now, renderContent will be called on setState , this will be responsible for appending Modal to imagePreviewContainer . Let’s add the final piece of code which will port Modal outside of its parent (Gallery).

And that’s it. Now if we click on any image it a modal opens up with the image in full screen mode. Lets see how the DOM tree looks when the modal is open:

Console

We can see here that Modal is ported outside of the AppContainer .

Let me know in comments below if you have used portals and what was it used for.

Passionate Web/App Developer @Amazon, #javascript #reactjs #react-native #ruby #rails #elixir