Simple abstraction for creating and managing new render trees

Usage no npm install needed!

<script type="module">
  import reactLayer from '';



Simple abstraction for creating and managing new render trees


npm i -S react-layer


A Layer is a simple class that manages the mounting and unmounting of a ReactElements to a specific container element. This is helpful for when, in the course of a react heirarchy you need to append a component outside of the current render tree. The canonical example of this pattern is overlays or modals that need to be appended to the document body.

Simply put this sort of like the React equivalent to jquery's appendTo ($('node').appendTo('body'))

new Layer(container, renderFn)

The Layer object takes two arguments: a container, which is a DOM node that the layer will be mounted too (such as the document.body), and render, a function that, when called, returns a ReactElement to render into the container

Layer.render(cb, parentComponent)

Mounts and Renders the return value of the function provided in the constructor (renderFn) to the container

Takes two optional arguments: cb, a callback that will be called after the layer has been rendered, and a parentComponent, which is a React Component that is the conceptual parent of the layer. If parentComponent is provided, its React context will be passed along to the layer.


Unmounts the component from the container, but leaves the mount point node in the DOM.


Unmounts, and removes all traces of the layer from the container. This calls unmount() so there is no need to call it in addition to destroy.

var Layer = require('react-layer')
var Modal = require('some-modal-component')

// here is a simple async alert()
function alert(message, callback) {
  var layer = new Layer(document.body, function renderModal(){
        return (
          <Modal show onHide={finish}>
              <button onClick={finish}>Close</button>

  // actually renders our Modal off of the document.body

  function finish(){
      layer.destroy() // unmount and remove the React Component tree

alert('hello there!')

If you want to create a component that represents the layer you can do that with a fairly simple Higher Order Component. The below example will tie the lifecycle of the Layer with the component that creates it. You can just provide a render method and the HOC will ensure that it is created and rendered at the right time.

let createLayeredComponent = render => class extends React.Component {

  static propTypes = {
    container: React.PropTypes.any

  componentWillUnmount () {
    this._layer = null

  componentDidUpdate() {

  componentDidMount() {

  _renderOverlay() {
    if (!this._layer)
      this._layer = new Layer(this.props.container || document.body, () => this._child)


  render() {
    this._child = render(this.props) //create the elements in render(), otherwise Owner can be lost
    return null;

// and In use....

var LayeredComponent = createLayeredComponent(function(props){
  return (
    <MyComponentIWantRenderedInALayer {...props}>