-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathReactSetStateTestComponent2.js
185 lines (147 loc) · 5.98 KB
/
ReactSetStateTestComponent2.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function getComponent2UpdatingSetStateLifeCycleCalls() {
return _component2UpdatingSetStateLifeCycleCalls;
}
var _component2UpdatingSetStateLifeCycleCalls = [];
function getComponent2NonUpdatingSetStateLifeCycleCalls() {
return _component2NonUpdatingSetStateLifeCycleCalls;
}
var _component2NonUpdatingSetStateLifeCycleCalls = [];
function getComponent2LatestJSCounter() {
return _component2Counter;
}
function getComponent2UpdatingRenderedCounter() {
return ReactDOM.findDOMNode(updatingInstance).textContent;
}
function getComponent2NonUpdatingRenderedCounter() {
return ReactDOM.findDOMNode(nonUpdatingInstance).textContent;
}
function getComponent2UpdatingErrorMessage() {
return ReactDOM.findDOMNode(updatingInstance).textContent;
}
function getComponent2NonUpdatingErrorMessage() {
return ReactDOM.findDOMNode(nonUpdatingInstance).textContent;
}
function staticLifecycleCallProxy(name, shouldUpdate){
shouldUpdate ? _component2UpdatingSetStateLifeCycleCalls.push(name) : _component2NonUpdatingSetStateLifeCycleCalls.push(name);
}
function getComponent2ErrorMessage(){
return _error.toString();
}
function getComponent2ErrorInfo(){
return _info.componentStack;
}
function getComponent2ErrorFromDerivedState(){
return _errorFromGetDerivedState.toString();
}
var _component2Counter;
var _shouldThrow;
var _shouldUpdate;
var _error;
var _info;
var _errorFromGetDerivedState;
class ReactSetStateTestComponent2 extends React.Component {
constructor(props) {
super(props);
_component2Counter = 1;
_shouldThrow = true;
_shouldUpdate = props.shouldUpdate;
this.state = {counter: _component2Counter, shouldThrow: _shouldThrow, error: '', info: '', errorFromGetDerivedState: ''};
}
recordStateChange(newCount) {
_component2Counter = newCount;
}
recordLifecycleCall(name) {
this.props.shouldUpdate ? _component2UpdatingSetStateLifeCycleCalls.push(name) : _component2NonUpdatingSetStateLifeCycleCalls.push(name);
this.recordStateChange(this.state.counter);
}
static getDerivedStateFromProps(nextProps, __) {
_shouldUpdate = nextProps.shouldUpdate;
_shouldUpdate
? _component2UpdatingSetStateLifeCycleCalls.push("getDerivedStateFromProps")
: _component2NonUpdatingSetStateLifeCycleCalls.push("getDerivedStateFromProps");
return null;
}
shouldComponentUpdate(_, __) {
this.recordLifecycleCall("shouldComponentUpdate");
_shouldUpdate = this.props.shouldUpdate;
return this.props.shouldUpdate;
}
getSnapshotBeforeUpdate(_, __) {
this.recordLifecycleCall("getSnapshotBeforeUpdate");
return null;
}
componentDidUpdate(_, __, ___) {
this.recordLifecycleCall("componentDidUpdate");
}
outerSetStateCallback() {
this.recordLifecycleCall('outerSetStateCallback');
}
innerSetStateCallback() {
this.recordLifecycleCall('innerSetStateCallback');
}
outerTransactionalSetStateCallback(previousState, props) {
this.recordLifecycleCall('outerTransactionalSetStateCallback');
return {counter: previousState.counter + 1};
}
innerTransactionalSetStateCallback(previousState, props) {
this.recordLifecycleCall('innerTransactionalSetStateCallback');
return {counter: previousState.counter + 1};
}
handleOuterClick(_) {
this.setState(this.outerTransactionalSetStateCallback.bind(this), this.outerSetStateCallback.bind(this));
}
handleInnerClick(_) {
this.setState(this.innerTransactionalSetStateCallback.bind(this), this.innerSetStateCallback.bind(this));
}
componentDidCatch(error, info) {
this.recordLifecycleCall('componentDidCatch');
_error = error;
_info = info;
this.setState({error, info});
}
static getDerivedStateFromError(error) {
staticLifecycleCallProxy('getDerivedStateFromError', _shouldUpdate);
_errorFromGetDerivedState = error;
return {shouldThrow: false, errorFromGetDerivedState: error};
}
render() {
this.recordLifecycleCall('render');
if (!this.state.shouldThrow) {
return React.createElement("div", {onClick: this.handleOuterClick.bind(this)},
[
React.createElement("div", {onClick: this.handleInnerClick.bind(this), key: 'c1'}, this.state.counter),
React.createElement("div", {onClick: this.handleInnerClick.bind(this), key: 'c2'}, this.state.error.toString()),
React.createElement("div", {onClick: this.handleInnerClick.bind(this), key: 'c3'}, this.state.info.toString()),
React.createElement("div", {onClick: this.handleInnerClick.bind(this), key: 'c4'}, this.state.errorFromGetDerivedState.toString()),
]
);
} else {
return React.createElement("div", {onClick: this.handleOuterClick.bind(this)},
[
React.createElement("div", {onClick: this.handleInnerClick.bind(this), key: 'c1'}, this.state.counter),
React.createElement(ErrorComponent, {key: 'c2'}, null)
]
);
}
}
}
class ErrorComponent extends React.Component {
throwError() {
throw "It crashed!";
}
render() {
this.throwError();
return React.createElement("div", {}, "Error");
}
}
ReactSetStateTestComponent2.defaultProps = { shouldUpdate: true };
var reactComponent2UpdatingInstance = ReactDOM.render(
React.createElement(ReactSetStateTestComponent2),
document.createElement("div")
);
var reactComponent2NonUpdatingInstance = ReactDOM.render(
React.createElement(ReactSetStateTestComponent2, {shouldUpdate: false}),
document.createElement("div")
);
React.addons.TestUtils.Simulate.click(ReactDOM.findDOMNode(reactComponent2UpdatingInstance).children[0]);
React.addons.TestUtils.Simulate.click(ReactDOM.findDOMNode(reactComponent2NonUpdatingInstance).children[0]);