-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathReactSetStateTestComponent.js
108 lines (82 loc) · 2.95 KB
/
ReactSetStateTestComponent.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
function getUpdatingSetStateLifeCycleCalls() {
return _updatingSetStateLifeCycleCalls;
}
var _updatingSetStateLifeCycleCalls = [];
function getNonUpdatingSetStateLifeCycleCalls() {
return _nonUpdatingSetStateLifeCycleCalls;
}
var _nonUpdatingSetStateLifeCycleCalls = [];
function getLatestJSCounter() {
return _counter;
}
function getUpdatingRenderedCounter() {
return ReactDOM.findDOMNode(updatingInstance).textContent;
}
function getNonUpdatingRenderedCounter() {
return ReactDOM.findDOMNode(nonUpdatingInstance).textContent;
}
var _counter;
class ReactSetStateTestComponent extends React.Component {
constructor(props) {
super(props);
_counter = 1;
this.state = {counter: _counter};
}
recordStateChange(newCount) {
_counter = newCount;
}
recordLifecycleCall(name) {
this.props.shouldUpdate ? _updatingSetStateLifeCycleCalls.push(name) : _nonUpdatingSetStateLifeCycleCalls.push(name);
this.recordStateChange(this.state.counter);
}
UNSAFE_componentWillReceiveProps(_) {
this.recordLifecycleCall("componentWillReceiveProps");
}
shouldComponentUpdate(_, __) {
this.recordLifecycleCall("shouldComponentUpdate");
return this.props.shouldUpdate;
}
UNSAFE_componentWillUpdate(_, __) {
this.recordLifecycleCall("componentWillUpdate");
}
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));
}
render() {
this.recordLifecycleCall('render');
return React.createElement("div", {onClick: this.handleOuterClick.bind(this)},
React.createElement("div", {onClick: this.handleInnerClick.bind(this)}, this.state.counter)
);
}
}
ReactSetStateTestComponent.defaultProps = { shouldUpdate: true };
var updatingInstance = ReactDOM.render(
React.createElement(ReactSetStateTestComponent),
document.createElement("div")
);
var nonUpdatingInstance = ReactDOM.render(
React.createElement(ReactSetStateTestComponent, {shouldUpdate: false}),
document.createElement("div")
);
React.addons.TestUtils.Simulate.click(ReactDOM.findDOMNode(updatingInstance).children[0]);
React.addons.TestUtils.Simulate.click(ReactDOM.findDOMNode(nonUpdatingInstance).children[0]);