Skip to content

Instantly share code, notes, and snippets.

@jdsharp
Created November 11, 2016 16:17
Show Gist options
  • Save jdsharp/bc2a136cc6274af4702aade113812d3e to your computer and use it in GitHub Desktop.
Save jdsharp/bc2a136cc6274af4702aade113812d3e to your computer and use it in GitHub Desktop.
React Unmanaged DOM Node Wrapping
const UnmanagedDOMContainer = React.createClass({
getDefaultProps() {
return {
nodes : null
};
},
componentWillReceiveProps(newProps) {
if ( !this.props.nodes && newProps.nodes ) {
this.appendNodes(newProps.nodes);
}
},
shouldComponentUpdate() {
return false;
},
componentDidMount() {
this.appendNodes(this.props.nodes);
},
appendNodes(nodes) {
if ( nodes ) {
let self = ReactDOM.findDOMNode(this);
self.appendChild(nodes);
}
},
render() {
return (<div></div>);
}
});
const MyWrapperComponent = React.createClass({
componentWillMount() {
// Kidnap the childnodes...
theChildNodes = ...
this.setState({
childNodes : theChildNodes
});
},
render() {
return (
<UnmanagedDOMContainer nodes={this.state.childNodes} />
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment