Last active
August 29, 2015 13:56
-
-
Save pjstein/8995787 to your computer and use it in GitHub Desktop.
layers-in-react
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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