Skip to content

Commit c1aa596

Browse files
committed
small internals refactoring
Small perf/clean tweaks: - avoid allocating an array for slice ([].slice) - avoid allocating an array when no nodes is selected - put the forEach function in a dedicated method.
1 parent 446b49a commit c1aa596

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "src/index.js",
77
"scripts": {
88
"test": "ava test",
9+
"start": "ava test --watch --fail-fast",
910
"prebuild": "rm -rf lib && mkdir -p lib",
1011
"build": "babel src --out-dir lib",
1112
"prepublish": "npm run build",

src/ElementPortal.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import React from 'react';
33
import ReactDOM from 'react-dom';
44
import createReactClass from 'create-react-class';
55

6+
const { slice } = Array.prototype;
7+
68
const noop = () => {};
79

810
const ElementPortal = createReactClass({
911
propTypes: {
10-
id: PropTypes.string,
1112
selector: PropTypes.string,
1213
mapDomNodeToProps: PropTypes.func,
1314
shouldReset: PropTypes.bool,
@@ -23,26 +24,32 @@ const ElementPortal = createReactClass({
2324
},
2425

2526
renderToNodes() {
27+
const nodes = this.props.selector
28+
? slice.call(document.querySelectorAll(this.props.selector))
29+
: null;
30+
31+
if (nodes) {
32+
nodes.forEach(this.renderNode);
33+
}
34+
},
35+
36+
renderNode(node) {
2637
const mapDomNodeToProps = this.props.mapDomNodeToProps || noop;
27-
const nodes = (this.props.selector && [].slice.call(document.querySelectorAll(this.props.selector))) || [];
28-
const { component } = this.props;
29-
30-
nodes.forEach(node => {
31-
if (this.props.shouldReset) {
32-
node.className = '';
33-
node.removeAttribute('style');
34-
}
35-
36-
const children = component
37-
? React.createElement(component, mapDomNodeToProps(node))
38-
: React.Children.only(this.props.children);
39-
40-
ReactDOM.unstable_renderSubtreeIntoContainer(
41-
this,
42-
children,
43-
node
44-
);
45-
});
38+
39+
if (this.props.shouldReset) {
40+
node.className = '';
41+
node.removeAttribute('style');
42+
}
43+
44+
const children = this.props.component
45+
? React.createElement(this.props.component, mapDomNodeToProps(node))
46+
: React.Children.only(this.props.children);
47+
48+
ReactDOM.unstable_renderSubtreeIntoContainer(
49+
this,
50+
children,
51+
node
52+
);
4653
},
4754

4855
render() {

0 commit comments

Comments
 (0)