Skip to content

Commit 93d8fac

Browse files
committed
Minifier-friendly references to properties
1 parent d5acece commit 93d8fac

File tree

1 file changed

+21
-38
lines changed

1 file changed

+21
-38
lines changed

src/React.js

+21-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,14 @@
11
import React from "react";
22

33
function createClass(baseClass) {
4-
function bindProperty(instance, prop, value) {
5-
switch (prop) {
6-
case "state":
7-
case "render":
8-
case "componentDidMount":
9-
case "componentWillUnmount":
10-
instance[prop] = value;
11-
break;
12-
13-
case "componentDidCatch":
14-
case "componentWillUpdate":
15-
case "shouldComponentUpdate":
16-
case "getSnapshotBeforeUpdate":
17-
instance[prop] = function (a, b) { return value(a)(b)(); };
18-
break;
19-
20-
case "componentDidUpdate":
21-
instance[prop] = function (a, b, c) { return value(a)(b)(c)(); };
22-
break;
23-
24-
case "unsafeComponentWillMount":
25-
instance["UNSAFE_componentWillMount"] = value;
26-
break;
27-
28-
case "unsafeComponentWillReceiveProps":
29-
instance["UNSAFE_componentWillReceiveProps"] = function (a) { return value(a)(); };
30-
break;
31-
32-
case "unsafeComponentWillUpdate":
33-
instance["UNSAFE_componentWillUpdate"] = function (a, b) { return value(a)(b)(); };
34-
break;
35-
36-
default:
37-
throw new Error("[purescript-react] Not a component property: " + prop);
4+
function curry2(f) {
5+
return f === undefined ? f : function (a, b) {
6+
return f(a)(b)
7+
}
8+
}
9+
function curry3(f) {
10+
return f === undefined ? f : function (a, b, c) {
11+
return f(a)(b)(c)
3812
}
3913
}
4014

@@ -43,10 +17,19 @@ function createClass(baseClass) {
4317
var Constructor = function (props) {
4418
baseClass.call(this, props);
4519
var spec = ctrFn(this)();
46-
// eslint-disable-next-line guard-for-in
47-
for (var k in spec) {
48-
bindProperty(this, k, spec[k]);
49-
}
20+
21+
this.state = spec.state;
22+
this.render = spec.render;
23+
this.componentDidMount = spec.componentDidMount;
24+
this.componentWillUnmount = spec.componentWillUnmount;
25+
this.componentDidCatch = curry2(spec.componentDidCatch);
26+
this.componentWillUpdate = curry2(spec.componentWillUpdate);
27+
this.shouldComponentUpdate = curry2(spec.shouldComponentUpdate);
28+
this.getSnapshotBeforeUpdate = curry2(spec.getSnapshotBeforeUpdate);
29+
this.componentDidUpdate = curry3(spec.componentDidUpdate);
30+
this.UNSAFE_componentWillMount = spec.unsafeComponentWillMount;
31+
this.UNSAFE_componentWillReceiveProps = curry2(spec.unsafeComponentWillReceiveProps);
32+
this.UNSAFE_componentWillUpdate = curry3(spec.unsafeComponentWillUpdate);
5033
};
5134

5235
Constructor.displayName = displayName;

0 commit comments

Comments
 (0)