-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (38 loc) · 1.46 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
const PropTypes = require("./lib/propTypes");
const React = require("react");
/*
* A function for comparing two sets of props for the keys given in list
*/
function diffProps (current, next, list) {
for (var i = 0; i < list.length; i += 1) {
if (current[list[i]] !== next[list[i]]) {
return true;
}
}
return false;
}
function getAffectsRenderingProps (propTypes) {
var renderProps = [];
for (var k in propTypes) {
if (propTypes[k].affectsRendering === true) {
renderProps.push(k);
}
}
return renderProps;
}
/*
* Because React checks that all component instances' prototypes includes React.Component, cloning
* React.Component and only modifying the clone will cause warnings. To get around this issue we
* modify the prototype of React.Component and account for components which do not wish to use the
* features of re-react.
*/
React.Component.prototype.shouldComponentUpdate = function (nextProps, nextState, nextContext) {
var constructor = Object.getPrototypeOf(this).constructor;
if (typeof(constructor.renderProps) === "undefined") {
constructor.renderProps = getAffectsRenderingProps(constructor.propTypes);
}
if (typeof(constructor.propTypes) === "undefined" || constructor.renderProps.length === 0) {
return true;
}
return this.state !== nextState || this.context !== nextContext || diffProps(this.props, nextProps, constructor.renderProps);
};