Created
June 12, 2016 10:08
-
-
Save grese/a7d9ee5ec5534583572ad232a82e24d8 to your computer and use it in GitHub Desktop.
A ReactJS component for rendering other React components into a "src-less" iframe
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
var Sandboxr = React.createClass({ | |
componentDidMount() { | |
const iframe = this.refs.frame; | |
const node = iframe.contentDocument.createElement('div'); | |
iframe.contentDocument.body.appendChild(node); | |
this._contentNode = node; | |
this._renderChildrenIntoContentNode(this.props); | |
}, | |
_renderChildrenIntoContentNode(props) { | |
ReactDOM.unstable_renderSubtreeIntoContainer( | |
this, | |
<div>{props.children}</div>, | |
this._contentNode | |
); | |
}, | |
componentWillUpdate(nextProps) { | |
this._renderChildrenIntoContentNode(nextProps); | |
}, | |
componentWillUnmount() { | |
ReactDOM.unmountComponentAtNode(this._contentNode); | |
}, | |
render() { | |
return <iframe | |
ref="frame" | |
width={this.props.width} | |
height={this.props.height} | |
/>; | |
} | |
}); | |
var Sample = React.createClass({ | |
render() { | |
return <div>{this.props.text}</div>; | |
} | |
}); | |
ReactDOM.render( | |
<Sandboxr width="250" height="250"> | |
<Sample text="Hello world!" /> | |
<Sample text="It's nice to see you again :P" /> | |
</Sandboxr>, | |
document.getElementById('container') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a sample of how one can render React components into an iframe. This can be useful for sandboxing styles/markup from other places.