-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (81 loc) · 2.39 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import React from "react";
export default function({
render,
getInitialState,
getDefaultProps,
propTypes,
displayName,
componentWillMount,
componentDidMount,
componentWillReceiveProps,
shouldComponentUpdate,
componentWillUpdate,
componentDidUpdate,
componentWillUnmount,
}) {
let componentSpec = {};
if (render) {
componentSpec.render = function() {
// this method will bind handlers to the correct this pointer;
// could be problematic because this'll be called on every render.
const handler = (impl) => {
return (e) => {
impl({props: this.props, state: this.state}, this.setState.bind(this));
}
}
return render({props: this.props, state: this.state}, handler);
}
}
if (getInitialState) {
componentSpec.getInitialState = function() {
return getInitialState();
}
}
if (getDefaultProps) {
componentSpec.getDefaultProps = function() {
return getDefaultProps();
}
}
if (propTypes) {
componentSpec.propTypes = propTypes;
}
if (displayName) {
componentSpec.displayName = displayName;
}
if (componentWillMount) {
componentSpec.componentWillMount = function() {
componentWillMount({props: this.props, state: this.state}, this.setState.bind(this));
}
}
if (componentDidMount) {
componentSpec.componentDidMount = function () {
componentDidMount({props: this.props, state: this.state});
}
}
if (componentWillReceiveProps) {
componentSpec.componentWillReceiveProps = function(nextProps) {
componentWillReceiveProps({props: this.props, state: this.state}, nextProps, this.setState.bind(this));
}
}
if (shouldComponentUpdate) {
componentSpec.shouldComponentUpdate = function(nextProps, nextState) {
return shouldComponentUpdate({props: this.props, state: this.state}, {props: nextProps, state:nextState});
}
}
if (componentWillUpdate) {
componentSpec.componentWillUpdate = function(nextProps, nextState) {
componentWillUpdate({props: this.props, state: this.state}, {props: nextProps, state:nextState});
}
}
if (componentDidUpdate) {
componentSpec.componentDidUpdate = function(prevProps, prevState) {
componentDidUpdate({props: this.props, state: this.state}, {props: prevProps, state:prevState});
}
}
if (componentWillUnmount) {
componentSpec.componentWillUnmount = function() {
componentWillUnmount({props: this.props, state: this.state});
}
}
return React.createClass(componentSpec);
};