-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
withProxy.js
36 lines (29 loc) · 833 Bytes
/
withProxy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import React, { PureComponent } from 'react';
import { render, unmountComponentAtNode } from 'react-dom';
export default function(Component) {
return class WithProxy extends PureComponent {
node = null;
componentDidMount() {
this.appendToBody(this.props);
}
componentWillUnmount() {
this.removeFromBody();
}
componentWillReceiveProps(nextProps) {
this.removeFromBody();
this.appendToBody(nextProps);
}
removeFromBody() {
unmountComponentAtNode(this.node);
document.body.removeChild(this.node);
}
appendToBody(props) {
const container = document.createElement('div');
this.node = document.body.appendChild(container);
render(<Component {...props} key={props.id} />, this.node);
}
render() {
return null;
}
};
}