/
ContainerRender.js
72 lines (65 loc) · 1.5 KB
/
ContainerRender.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React from 'react';
import ReactDOM from 'react-dom';
/**
* @deprecated Since we do not need support React15 any more.
* Will remove in next major version.
*/
export default class ContainerRender extends React.Component {
static defaultProps = {
autoMount: true,
autoDestroy: true,
forceRender: false,
};
componentDidMount() {
if (this.props.autoMount) {
this.renderComponent();
}
}
componentDidUpdate() {
if (this.props.autoMount) {
this.renderComponent();
}
}
componentWillUnmount() {
if (this.props.autoDestroy) {
this.removeContainer();
}
}
removeContainer = () => {
if (this.container) {
ReactDOM.unmountComponentAtNode(this.container);
this.container.parentNode.removeChild(this.container);
this.container = null;
}
};
renderComponent = (props, ready) => {
const {
visible,
getComponent,
forceRender,
getContainer,
parent,
} = this.props;
if (visible || parent._component || forceRender) {
if (!this.container) {
this.container = getContainer();
}
ReactDOM.unstable_renderSubtreeIntoContainer(
parent,
getComponent(props),
this.container,
function callback() {
if (ready) {
ready.call(this);
}
},
);
}
};
render() {
return this.props.children({
renderComponent: this.renderComponent,
removeContainer: this.removeContainer,
});
}
}