-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (38 loc) · 1.2 KB
/
index.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
import React from 'react';
const PREFIX_KEEP = 'keep_';
const PREFIX_KEEP_LENGTH = PREFIX_KEEP.length;
export default class Compor extends React.Component {
render() {
const Component = this.props.ctype;
const props = this.getRealProps();
return <Component {...props} />;
}
shouldComponentUpdate(nextProps) {
const prevKeys = Object.keys(this.props);
const nextKeys = Object.keys(nextProps);
if (prevKeys.length !== nextKeys.length) return true;
for (let key of nextKeys) {
if (key.startsWith(PREFIX_KEEP)) continue;
if (key === 'children' && typeof(this.props[key]) === 'function') continue;
if (this.props[key] !== nextProps[key]) return true;
}
return false;
}
getRealProps = () => {
const props = {};
Object.keys(this.props).forEach(key => {
if (key === 'ctype') {
// do nothing
} else if (key.startsWith(PREFIX_KEEP)) {
props[key.substr(PREFIX_KEEP_LENGTH)] = this.props[key];
} else {
props[key] = this.props[key];
}
});
if (typeof(props.children) === 'function') {
props.children = props.children();
}
return props;
}
}
export const Nothing = props => props.children;