Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e43d27b

Browse files
BinaryMusekuychaco
authored andcommitted
Convert to new ReactDOM.createPortal
1 parent d815c4c commit e43d27b

File tree

1 file changed

+15
-27
lines changed

1 file changed

+15
-27
lines changed

lib/views/portal.js

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import PropTypes from 'prop-types';
1212
*
1313
* Given the above example, there will be a span with the class "portal-class"
1414
* 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`
1616
* to preserve context in the subtree.
1717
*
1818
* `getElement()` allows access to the React subtree container element.
@@ -29,6 +29,7 @@ export default class Portal extends React.Component {
2929
className: PropTypes.string,
3030
appendNode: PropTypes.bool,
3131
getDOMNode: PropTypes.func,
32+
children: PropTypes.element.isRequired,
3233
}
3334

3435
static defaultProps = {
@@ -38,56 +39,43 @@ export default class Portal extends React.Component {
3839
getDOMNode: null,
3940
}
4041

41-
componentDidMount() {
42+
constructor(props, context) {
43+
super(props, context);
44+
4245
let node;
43-
if (this.props.getDOMNode) {
44-
node = this.props.getDOMNode();
46+
if (props.getDOMNode) {
47+
node = props.getDOMNode();
4548
}
4649

4750
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;
5053
}
5154

5255
this.node = node;
5356

54-
if (this.props.appendNode) {
55-
document.body.appendChild(this.node);
57+
if (props.appendNode) {
58+
document.body.appendChild(node);
5659
}
57-
this.renderPortal(this.props);
58-
}
59-
60-
componentWillReceiveProps(newProps) {
61-
this.renderPortal(newProps);
6260
}
6361

6462
componentWillUnmount() {
65-
ReactDom.unmountComponentAtNode(this.node);
6663
if (this.props.appendNode) {
67-
document.body.removeChild(this.node);
64+
document.body.removeChild(this.state.node);
6865
}
6966
}
7067

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-
8168
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);
8371
}
8472

8573
getRenderedSubtree() {
8674
return this.subtree;
8775
}
8876

8977
getElement() {
90-
return this.node;
78+
return this.state.node;
9179
}
9280

9381
getView() {

0 commit comments

Comments
 (0)