Skip to content

Instantly share code, notes, and snippets.

@swcho
Created March 14, 2017 09:15
Show Gist options
  • Save swcho/37104c7065ed1cb728ca57f365f98c46 to your computer and use it in GitHub Desktop.
Save swcho/37104c7065ed1cb728ca57f365f98c46 to your computer and use it in GitHub Desktop.
Dynamic hoc
height = -1;
leftMenu;
registerHeightChange(element: React.ReactElement<{}>) {
const Component = element.type['WrappedComponent'];
const orig = Component.prototype.componentDidUpdate;
const thiz = this;
Component.prototype.componentDidUpdate = function() {
// console.log(this);
const self = ReactDOM.findDOMNode(this);
if (thiz.height !== self.clientHeight) {
thiz.height = self.clientHeight;
console.log('height', thiz.height);
if (thiz.leftMenu) {
thiz.leftMenu['forceUpdate']();
}
}
// console.log(thiz);
if (orig) {
orig.call(this);
}
};
return element;
}
render() {
return (
{ this.registerHeightChange(this.props.children) }
...
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment