There is a modal in this answer https://stackoverflow.com/a/26789089/883571 which is creating a React-based Modal by appending it to <body>
. However, I found it not compatible with the transition addons provided by React.
How to create one with transitions(during enter and leave)?
The fundamental problem here is that in React you're only allowed to mount component to its parent, which is not always the desired behavior. But how to address this issue?
I've made the solution, addressed to fix this issue. More detailed problem definition, src and examples can be found here: https://github.com/fckt/react-layer-stack#rationale
Take a look at https://github.com/fckt/react-layer-stack/blob/master/README.md#real-world-usage-example to see the concrete example which is answer to your question:
At react conf 2015, Ryan Florence demonstrated using portals. Here's how you can create a simple
Portal
component...and then everything you can normally do in React you can do inside of the portal...
jsbin demo
You can also have a look at Ryan's react-modal, although I haven't actually used it so I don't know how well it works with animation.
Hope it helps. This is my current implementation of a transition modal based on the anwser above:
React 15.x
Here's an ES6 version of the method described in this article:
Just wrap any elements you want to be at the end of the DOM with it:
React 16.x
Here's an updated version for React 16:
Working example
As other answers have stated this can be done using Portals. Starting from
v16.0
Portals are included in React.Normally, when you return an element from a component's render method, it's mounted into the DOM as a child of the nearest parent node, but with portals you can insert a child into a different location in the DOM.
Check working example here.
I wrote the module react-portal that should help you.