@@ -12,7 +12,7 @@ import PropTypes from 'prop-types';
12
12
*
13
13
* Given the above example, there will be a span with the class "portal-class"
14
14
* created and appended to the document body, and then `<Stuff />` will be
15
- * rendered into it. Note that this uses `unstable_renderSubtreeIntoContainer `
15
+ * rendered into it. Note that this uses `createPortal `
16
16
* to preserve context in the subtree.
17
17
*
18
18
* `getElement()` allows access to the React subtree container element.
@@ -29,6 +29,7 @@ export default class Portal extends React.Component {
29
29
className : PropTypes . string ,
30
30
appendNode : PropTypes . bool ,
31
31
getDOMNode : PropTypes . func ,
32
+ children : PropTypes . element . isRequired ,
32
33
}
33
34
34
35
static defaultProps = {
@@ -38,56 +39,43 @@ export default class Portal extends React.Component {
38
39
getDOMNode : null ,
39
40
}
40
41
41
- componentDidMount ( ) {
42
+ constructor ( props , context ) {
43
+ super ( props , context ) ;
44
+
42
45
let node ;
43
- if ( this . props . getDOMNode ) {
44
- node = this . props . getDOMNode ( ) ;
46
+ if ( props . getDOMNode ) {
47
+ node = props . getDOMNode ( ) ;
45
48
}
46
49
47
50
if ( ! node ) {
48
- node = document . createElement ( this . props . type ) ;
49
- node . className = this . props . className ;
51
+ node = document . createElement ( props . type ) ;
52
+ node . className = props . className ;
50
53
}
51
54
52
55
this . node = node ;
53
56
54
- if ( this . props . appendNode ) {
55
- document . body . appendChild ( this . node ) ;
57
+ if ( props . appendNode ) {
58
+ document . body . appendChild ( node ) ;
56
59
}
57
- this . renderPortal ( this . props ) ;
58
- }
59
-
60
- componentWillReceiveProps ( newProps ) {
61
- this . renderPortal ( newProps ) ;
62
60
}
63
61
64
62
componentWillUnmount ( ) {
65
- ReactDom . unmountComponentAtNode ( this . node ) ;
66
63
if ( this . props . appendNode ) {
67
- document . body . removeChild ( this . node ) ;
64
+ document . body . removeChild ( this . state . node ) ;
68
65
}
69
66
}
70
67
71
- renderPortal ( props ) {
72
- this . subtree = ReactDom . unstable_renderSubtreeIntoContainer (
73
- this , props . children , this . node ,
74
- ) ;
75
- }
76
-
77
- shouldComponentUpdate ( ) {
78
- return false ;
79
- }
80
-
81
68
render ( ) {
82
- return null ;
69
+ const child = React . Children . only ( this . props . children ) ;
70
+ return ReactDom . createPortal ( React . cloneElement ( child , { ref : c => { this . subtree = c ; } } ) , this . node ) ;
83
71
}
84
72
85
73
getRenderedSubtree ( ) {
86
74
return this . subtree ;
87
75
}
88
76
89
77
getElement ( ) {
90
- return this . node ;
78
+ return this . state . node ;
91
79
}
92
80
93
81
getView ( ) {
0 commit comments