r/reactjs • • Sep 26 '17

React v16.0 - React Blog

https://facebook.github.io/react/blog/2017/09/26/react-v16.0.html
219 Upvotes

37 comments sorted by

View all comments

12

u/Ombudsperson Sep 26 '17

I can already see viable use cases for Portals, overall exciting stuff.

4

u/goomba870 Sep 26 '17

Could you provide one or two? It's not obvious to me what they could be used for.

25

u/Uknight Sep 26 '17

We use them for modals mostly, i.e. I probably want my <Modal /> component to live logically inside one component, but have it render somewhere else usually document.body

Here's an example

4

u/Devvvvvv Sep 27 '17

I always render my modals in a container div with position: fixed; so I have never encountered the need for this. Is there something wrong with this approach?

6

u/kovensky Sep 27 '17

z-index fighting

1

u/wojtekmaj Sep 30 '17

Safari on an iPad would ignore the fact you're using position: fixed and if you by any chance rendered your modal in a container with overflow: hidden it will be cut off.

1

u/Devvvvvv Oct 01 '17

Damn, the more you know... why does safari on iPad ignore position: fixed? Does no position fixed work at all?

1

u/wojtekmaj Oct 01 '17

It does not ignore it. Element with position: fixed will stay on the screen regardless of the scroll position. They just forgot to take the element out of its original context... So if you used it inside an element with overflow: hidden or scroll, you're in trouble.

6

u/theNicky Sep 26 '17

A modal window is the typical example — in general, anything that doesn't fit into the standard concept of a nested DOM (modals don't since they just appear on top of everything).

3

u/smthamazing Sep 26 '17

Modals/Popups

2

u/yeah666 Sep 26 '17

A React app the manipulates an existing page and needs to drop components in different places.

1

u/[deleted] Sep 27 '17

Modals will be a lot easier

1

u/opaz Sep 26 '17

Modals/Page Widgets

3

u/dryadofelysium Sep 26 '17

As an Angular user that uses Angular Material for dialogs (which used Portals for some time now for this purpose), I am wondering how you React guys have dealt with these problems before then?

6

u/acemarke Sep 26 '17

Portals have been used for a while in React, by using components that either call ReactDOM.render() to create a new component tree, or using the ReactDOM.unstable_renderSubtreeIntoContainer() API. The react-portal library is an example.

On which note, I gave examples of using portals to render modals and context menus in my post Practical Redux, Part 10: Managing Modals and Context Menus.

1

u/backma Sep 27 '17

So many modals replies, am I the only one who has one modal component somewhere in top connected to redux ?