Skip to content

Instantly share code, notes, and snippets.

@pjstein
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjstein/8995787 to your computer and use it in GitHub Desktop.
Save pjstein/8995787 to your computer and use it in GitHub Desktop.
layers-in-react
/*
Instagram is 100% React. They pop modals. I didn't know how you could
do that in React, so I guessed based on what was going on the in
Chrome inspector. The following code assumes a commonJS module system.
Classes that I pass into this use the this.props.teardown to take themselves
down.
*/
var _ = require('lodash'),
React = require('react');
var count = 0;
function gimmeATeardownFn() {
React.unmountComponentAtNode(this);
this.parentNode.removeChild(this);
}
function throwItOnALayer(componentClass, properties) {
var layer = document.createElement('div'),
teardownfn = gimmeATeardownFn.bind(layer);
// using lodash, assign works just like 'extend' does in underscore.
React.renderComponent(
componentClass(_.assign({ teardown : teardownfn }, properties)), layer
);
document.body.appendChild(layer);
}
module.exports = throwItOnALayer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment