30
31
TestComponent = React.createClass({
32
peekAtState: function(from, state) {
33
- if (state) {
34
- this.props.stateListener(from, state && state.color);
35
- } else {
36
- var internalInstance = ReactInstanceMap.get(this);
37
- var pendingState = internalInstance ? internalInstance._pendingState :
38
- null;
39
- this.props.stateListener(
40
- from,
41
- this.state && this.state.color,
42
- pendingState && pendingState.color
43
- );
44
- }
33
+ state = state || this.state;
34
+ this.props.stateListener(from, state && state.color);
35
},
36
37
peekAtCallback: function(from) {
57
58
componentWillMount: function() {
59
this.peekAtState('componentWillMount-start');
60
+ this.setState(function (state) {
61
+ this.peekAtState('before-setState-sunrise', state);
62
+ });
63
this.setState(
64
{color: 'sunrise'},
65
this.peekAtCallback('setState-sunrise')
66
);
67
+ this.setState(function (state) {
68
+ this.peekAtState('after-setState-sunrise', state);
69
+ });
70
this.peekAtState('componentWillMount-after-sunrise');
71
this.setState(
72
{color: 'orange'},
73
this.peekAtCallback('setState-orange')
74
);
75
+ this.setState(function (state) {
76
+ this.peekAtState('after-setState-orange', state);
77
+ });
78
this.peekAtState('componentWillMount-end');
79
},
80
90
componentWillReceiveProps: function(newProps) {
91
this.peekAtState('componentWillReceiveProps-start');
92
if (newProps.nextColor) {
93
+ this.setState(function (state) {
94
+ this.peekAtState('before-setState-receiveProps', state);
95
+ return {color: newProps.nextColor};
96
+ });
97
+ this.replaceState({ color: undefined });
98
this.setState(
95
- {color: newProps.nextColor},
99
+ function (state) {
100
+ this.peekAtState('before-setState-again-receiveProps', state);
101
+ return {color: newProps.nextColor};
102
+ },
103
this.peekAtCallback('setState-receiveProps')
104
);
105
+ this.setState(function (state) {
106
+ this.peekAtState('after-setState-receiveProps', state);
107
+ });
108
}
109
this.peekAtState('componentWillReceiveProps-end');
110
},
155
156
expect(stateListener.mock.calls).toEqual([
157
// there is no state when getInitialState() is called
148
- [ 'getInitialState', null, null ],
149
- [ 'componentWillMount-start', 'red', null ],
150
- // setState() only enqueues a pending state.
151
- [ 'componentWillMount-after-sunrise', 'red', 'sunrise' ],
152
- [ 'componentWillMount-end', 'red', 'orange' ],
158
+ [ 'getInitialState', null ],
159
+ [ 'componentWillMount-start', 'red' ],
160
+ // setState()'s only enqueue pending states.
161
+ [ 'componentWillMount-after-sunrise', 'red' ],
162
+ [ 'componentWillMount-end', 'red' ],
163
+ // pending state queue is processed
164
+ [ 'before-setState-sunrise', 'red' ],
165
+ [ 'after-setState-sunrise', 'sunrise' ],
166
+ [ 'after-setState-orange', 'orange' ],
167
// pending state has been applied
154
- [ 'render', 'orange', null ],
155
- [ 'componentDidMount-start', 'orange', null ],
168
+ [ 'render', 'orange' ],
169
+ [ 'componentDidMount-start', 'orange' ],
170
// setState-sunrise and setState-orange should be called here,
171
// after the bug in #1740
172
// componentDidMount() called setState({color:'yellow'}), which is async.
173
// The update doesn't happen until the next flush.
160
- [ 'componentDidMount-end', 'orange', 'yellow' ],
161
- [ 'shouldComponentUpdate-currentState', 'orange', null ],
174
+ [ 'componentDidMount-end', 'orange' ],
175
+ [ 'shouldComponentUpdate-currentState', 'orange' ],
176
[ 'shouldComponentUpdate-nextState', 'yellow' ],
163
- [ 'componentWillUpdate-currentState', 'orange', null ],
177
+ [ 'componentWillUpdate-currentState', 'orange' ],
178
[ 'componentWillUpdate-nextState', 'yellow' ],
165
- [ 'render', 'yellow', null ],
166
- [ 'componentDidUpdate-currentState', 'yellow', null ],
179
+ [ 'render', 'yellow' ],
180
+ [ 'componentDidUpdate-currentState', 'yellow' ],
181
[ 'componentDidUpdate-prevState', 'orange' ],
168
- [ 'setState-yellow', 'yellow', null ],
169
- [ 'initial-callback', 'yellow', null ],
170
- [ 'componentWillReceiveProps-start', 'yellow', null ],
182
+ [ 'setState-yellow', 'yellow' ],
183
+ [ 'initial-callback', 'yellow' ],
184
+ [ 'componentWillReceiveProps-start', 'yellow' ],
185
// setState({color:'green'}) only enqueues a pending state.
172
- [ 'componentWillReceiveProps-end', 'yellow', 'green' ],
173
- [ 'shouldComponentUpdate-currentState', 'yellow', null ],
186
+ [ 'componentWillReceiveProps-end', 'yellow' ],
187
+ // pending state queue is processed
188
+ // before-setState-receiveProps never called, due to replaceState.
189
+ [ 'before-setState-again-receiveProps', undefined ],
190
+ [ 'after-setState-receiveProps', 'green' ],
191
+ [ 'shouldComponentUpdate-currentState', 'yellow' ],
192
[ 'shouldComponentUpdate-nextState', 'green' ],
175
- [ 'componentWillUpdate-currentState', 'yellow', null ],
193
+ [ 'componentWillUpdate-currentState', 'yellow' ],
194
[ 'componentWillUpdate-nextState', 'green' ],
177
- [ 'render', 'green', null ],
178
- [ 'componentDidUpdate-currentState', 'green', null ],
195
+ [ 'render', 'green' ],
196
+ [ 'componentDidUpdate-currentState', 'green' ],
197
[ 'componentDidUpdate-prevState', 'yellow' ],
180
- [ 'setState-receiveProps', 'green', null ],
181
- [ 'setProps', 'green', null ],
198
+ [ 'setState-receiveProps', 'green' ],
199
+ [ 'setProps', 'green' ],
200
// setFavoriteColor('blue')
183
- [ 'shouldComponentUpdate-currentState', 'green', null ],
201
+ [ 'shouldComponentUpdate-currentState', 'green' ],
202
[ 'shouldComponentUpdate-nextState', 'blue' ],
185
- [ 'componentWillUpdate-currentState', 'green', null ],
203
+ [ 'componentWillUpdate-currentState', 'green' ],
204
[ 'componentWillUpdate-nextState', 'blue' ],
187
- [ 'render', 'blue', null ],
188
- [ 'componentDidUpdate-currentState', 'blue', null ],
205
+ [ 'render', 'blue' ],
206
+ [ 'componentDidUpdate-currentState', 'blue' ],
207
[ 'componentDidUpdate-prevState', 'green' ],
190
- [ 'setFavoriteColor', 'blue', null ],
208
+ [ 'setFavoriteColor', 'blue' ],
209
// forceUpdate()
192
- [ 'componentWillUpdate-currentState', 'blue', null ],
210
+ [ 'componentWillUpdate-currentState', 'blue' ],
211
[ 'componentWillUpdate-nextState', 'blue' ],
194
- [ 'render', 'blue', null ],
195
- [ 'componentDidUpdate-currentState', 'blue', null ],
212
+ [ 'render', 'blue' ],
213
+ [ 'componentDidUpdate-currentState', 'blue' ],
214
[ 'componentDidUpdate-prevState', 'blue' ],
197
- [ 'forceUpdate', 'blue', null ],
215
+ [ 'forceUpdate', 'blue' ],
216
// unmountComponent()
217
// state is available within `componentWillUnmount()`
200
- [ 'componentWillUnmount', 'blue', null ]
218
+ [ 'componentWillUnmount', 'blue' ]
219
]);
220
});
221
});