1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant'),
|
14 | _assign = require('object-assign');
|
15 |
|
16 | var React = require('react/lib/React');
|
17 | var ReactComponentEnvironment = require('./ReactComponentEnvironment');
|
18 | var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
|
19 | var ReactErrorUtils = require('./ReactErrorUtils');
|
20 | var ReactInstanceMap = require('./ReactInstanceMap');
|
21 | var ReactInstrumentation = require('./ReactInstrumentation');
|
22 | var ReactNodeTypes = require('./ReactNodeTypes');
|
23 | var ReactReconciler = require('./ReactReconciler');
|
24 |
|
25 | if (process.env.NODE_ENV !== 'production') {
|
26 | var checkReactTypeSpec = require('./checkReactTypeSpec');
|
27 | }
|
28 |
|
29 | var emptyObject = require('fbjs/lib/emptyObject');
|
30 | var invariant = require('fbjs/lib/invariant');
|
31 | var shallowEqual = require('fbjs/lib/shallowEqual');
|
32 | var shouldUpdateReactComponent = require('./shouldUpdateReactComponent');
|
33 | var warning = require('fbjs/lib/warning');
|
34 |
|
35 | var CompositeTypes = {
|
36 | ImpureClass: 0,
|
37 | PureClass: 1,
|
38 | StatelessFunctional: 2
|
39 | };
|
40 |
|
41 | function StatelessComponent(Component) {}
|
42 | StatelessComponent.prototype.render = function () {
|
43 | var Component = ReactInstanceMap.get(this)._currentElement.type;
|
44 | var element = Component(this.props, this.context, this.updater);
|
45 | warnIfInvalidElement(Component, element);
|
46 | return element;
|
47 | };
|
48 |
|
49 | function warnIfInvalidElement(Component, element) {
|
50 | if (process.env.NODE_ENV !== 'production') {
|
51 | process.env.NODE_ENV !== 'production' ? warning(element === null || element === false || React.isValidElement(element), '%s(...): A valid React element (or null) must be returned. You may have ' + 'returned undefined, an array or some other invalid object.', Component.displayName || Component.name || 'Component') : void 0;
|
52 | process.env.NODE_ENV !== 'production' ? warning(!Component.childContextTypes, '%s(...): childContextTypes cannot be defined on a functional component.', Component.displayName || Component.name || 'Component') : void 0;
|
53 | }
|
54 | }
|
55 |
|
56 | function shouldConstruct(Component) {
|
57 | return !!(Component.prototype && Component.prototype.isReactComponent);
|
58 | }
|
59 |
|
60 | function isPureComponent(Component) {
|
61 | return !!(Component.prototype && Component.prototype.isPureReactComponent);
|
62 | }
|
63 |
|
64 |
|
65 | function measureLifeCyclePerf(fn, debugID, timerType) {
|
66 | if (debugID === 0) {
|
67 |
|
68 |
|
69 |
|
70 | return fn();
|
71 | }
|
72 |
|
73 | ReactInstrumentation.debugTool.onBeginLifeCycleTimer(debugID, timerType);
|
74 | try {
|
75 | return fn();
|
76 | } finally {
|
77 | ReactInstrumentation.debugTool.onEndLifeCycleTimer(debugID, timerType);
|
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 | var nextMountID = 1;
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | var ReactCompositeComponent = {
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | construct: function (element) {
|
128 | this._currentElement = element;
|
129 | this._rootNodeID = 0;
|
130 | this._compositeType = null;
|
131 | this._instance = null;
|
132 | this._hostParent = null;
|
133 | this._hostContainerInfo = null;
|
134 |
|
135 |
|
136 | this._updateBatchNumber = null;
|
137 | this._pendingElement = null;
|
138 | this._pendingStateQueue = null;
|
139 | this._pendingReplaceState = false;
|
140 | this._pendingForceUpdate = false;
|
141 |
|
142 | this._renderedNodeType = null;
|
143 | this._renderedComponent = null;
|
144 | this._context = null;
|
145 | this._mountOrder = 0;
|
146 | this._topLevelWrapper = null;
|
147 |
|
148 |
|
149 | this._pendingCallbacks = null;
|
150 |
|
151 |
|
152 | this._calledComponentWillUnmount = false;
|
153 |
|
154 | if (process.env.NODE_ENV !== 'production') {
|
155 | this._warnedAboutRefsInRender = false;
|
156 | }
|
157 | },
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | mountComponent: function (transaction, hostParent, hostContainerInfo, context) {
|
171 | var _this = this;
|
172 |
|
173 | this._context = context;
|
174 | this._mountOrder = nextMountID++;
|
175 | this._hostParent = hostParent;
|
176 | this._hostContainerInfo = hostContainerInfo;
|
177 |
|
178 | var publicProps = this._currentElement.props;
|
179 | var publicContext = this._processContext(context);
|
180 |
|
181 | var Component = this._currentElement.type;
|
182 |
|
183 | var updateQueue = transaction.getUpdateQueue();
|
184 |
|
185 |
|
186 | var doConstruct = shouldConstruct(Component);
|
187 | var inst = this._constructComponent(doConstruct, publicProps, publicContext, updateQueue);
|
188 | var renderedElement;
|
189 |
|
190 |
|
191 | if (!doConstruct && (inst == null || inst.render == null)) {
|
192 | renderedElement = inst;
|
193 | warnIfInvalidElement(Component, renderedElement);
|
194 | !(inst === null || inst === false || React.isValidElement(inst)) ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s(...): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.', Component.displayName || Component.name || 'Component') : _prodInvariant('105', Component.displayName || Component.name || 'Component') : void 0;
|
195 | inst = new StatelessComponent(Component);
|
196 | this._compositeType = CompositeTypes.StatelessFunctional;
|
197 | } else {
|
198 | if (isPureComponent(Component)) {
|
199 | this._compositeType = CompositeTypes.PureClass;
|
200 | } else {
|
201 | this._compositeType = CompositeTypes.ImpureClass;
|
202 | }
|
203 | }
|
204 |
|
205 | if (process.env.NODE_ENV !== 'production') {
|
206 |
|
207 |
|
208 | if (inst.render == null) {
|
209 | process.env.NODE_ENV !== 'production' ? warning(false, '%s(...): No `render` method found on the returned component ' + 'instance: you may have forgotten to define `render`.', Component.displayName || Component.name || 'Component') : void 0;
|
210 | }
|
211 |
|
212 | var propsMutated = inst.props !== publicProps;
|
213 | var componentName = Component.displayName || Component.name || 'Component';
|
214 |
|
215 | process.env.NODE_ENV !== 'production' ? warning(inst.props === undefined || !propsMutated, '%s(...): When calling super() in `%s`, make sure to pass ' + "up the same props that your component's constructor was passed.", componentName, componentName) : void 0;
|
216 | }
|
217 |
|
218 |
|
219 |
|
220 | inst.props = publicProps;
|
221 | inst.context = publicContext;
|
222 | inst.refs = emptyObject;
|
223 | inst.updater = updateQueue;
|
224 |
|
225 | this._instance = inst;
|
226 |
|
227 |
|
228 | ReactInstanceMap.set(inst, this);
|
229 |
|
230 | if (process.env.NODE_ENV !== 'production') {
|
231 |
|
232 |
|
233 |
|
234 | process.env.NODE_ENV !== 'production' ? warning(!inst.getInitialState || inst.getInitialState.isReactClassApproved || inst.state, 'getInitialState was defined on %s, a plain JavaScript class. ' + 'This is only supported for classes created using React.createClass. ' + 'Did you mean to define a state property instead?', this.getName() || 'a component') : void 0;
|
235 | process.env.NODE_ENV !== 'production' ? warning(!inst.getDefaultProps || inst.getDefaultProps.isReactClassApproved, 'getDefaultProps was defined on %s, a plain JavaScript class. ' + 'This is only supported for classes created using React.createClass. ' + 'Use a static property to define defaultProps instead.', this.getName() || 'a component') : void 0;
|
236 | process.env.NODE_ENV !== 'production' ? warning(!inst.propTypes, 'propTypes was defined as an instance property on %s. Use a static ' + 'property to define propTypes instead.', this.getName() || 'a component') : void 0;
|
237 | process.env.NODE_ENV !== 'production' ? warning(!inst.contextTypes, 'contextTypes was defined as an instance property on %s. Use a ' + 'static property to define contextTypes instead.', this.getName() || 'a component') : void 0;
|
238 | process.env.NODE_ENV !== 'production' ? warning(typeof inst.componentShouldUpdate !== 'function', '%s has a method called ' + 'componentShouldUpdate(). Did you mean shouldComponentUpdate()? ' + 'The name is phrased as a question because the function is ' + 'expected to return a value.', this.getName() || 'A component') : void 0;
|
239 | process.env.NODE_ENV !== 'production' ? warning(typeof inst.componentDidUnmount !== 'function', '%s has a method called ' + 'componentDidUnmount(). But there is no such lifecycle method. ' + 'Did you mean componentWillUnmount()?', this.getName() || 'A component') : void 0;
|
240 | process.env.NODE_ENV !== 'production' ? warning(typeof inst.componentWillRecieveProps !== 'function', '%s has a method called ' + 'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?', this.getName() || 'A component') : void 0;
|
241 | }
|
242 |
|
243 | var initialState = inst.state;
|
244 | if (initialState === undefined) {
|
245 | inst.state = initialState = null;
|
246 | }
|
247 | !(typeof initialState === 'object' && !Array.isArray(initialState)) ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s.state: must be set to an object or null', this.getName() || 'ReactCompositeComponent') : _prodInvariant('106', this.getName() || 'ReactCompositeComponent') : void 0;
|
248 |
|
249 | this._pendingStateQueue = null;
|
250 | this._pendingReplaceState = false;
|
251 | this._pendingForceUpdate = false;
|
252 |
|
253 | var markup;
|
254 | if (inst.unstable_handleError) {
|
255 | markup = this.performInitialMountWithErrorHandling(renderedElement, hostParent, hostContainerInfo, transaction, context);
|
256 | } else {
|
257 | markup = this.performInitialMount(renderedElement, hostParent, hostContainerInfo, transaction, context);
|
258 | }
|
259 |
|
260 | if (inst.componentDidMount) {
|
261 | if (process.env.NODE_ENV !== 'production') {
|
262 | transaction.getReactMountReady().enqueue(function () {
|
263 | measureLifeCyclePerf(function () {
|
264 | return inst.componentDidMount();
|
265 | }, _this._debugID, 'componentDidMount');
|
266 | });
|
267 | } else {
|
268 | transaction.getReactMountReady().enqueue(inst.componentDidMount, inst);
|
269 | }
|
270 | }
|
271 |
|
272 | return markup;
|
273 | },
|
274 |
|
275 | _constructComponent: function (doConstruct, publicProps, publicContext, updateQueue) {
|
276 | if (process.env.NODE_ENV !== 'production') {
|
277 | ReactCurrentOwner.current = this;
|
278 | try {
|
279 | return this._constructComponentWithoutOwner(doConstruct, publicProps, publicContext, updateQueue);
|
280 | } finally {
|
281 | ReactCurrentOwner.current = null;
|
282 | }
|
283 | } else {
|
284 | return this._constructComponentWithoutOwner(doConstruct, publicProps, publicContext, updateQueue);
|
285 | }
|
286 | },
|
287 |
|
288 | _constructComponentWithoutOwner: function (doConstruct, publicProps, publicContext, updateQueue) {
|
289 | var Component = this._currentElement.type;
|
290 |
|
291 | if (doConstruct) {
|
292 | if (process.env.NODE_ENV !== 'production') {
|
293 | return measureLifeCyclePerf(function () {
|
294 | return new Component(publicProps, publicContext, updateQueue);
|
295 | }, this._debugID, 'ctor');
|
296 | } else {
|
297 | return new Component(publicProps, publicContext, updateQueue);
|
298 | }
|
299 | }
|
300 |
|
301 |
|
302 |
|
303 | if (process.env.NODE_ENV !== 'production') {
|
304 | return measureLifeCyclePerf(function () {
|
305 | return Component(publicProps, publicContext, updateQueue);
|
306 | }, this._debugID, 'render');
|
307 | } else {
|
308 | return Component(publicProps, publicContext, updateQueue);
|
309 | }
|
310 | },
|
311 |
|
312 | performInitialMountWithErrorHandling: function (renderedElement, hostParent, hostContainerInfo, transaction, context) {
|
313 | var markup;
|
314 | var checkpoint = transaction.checkpoint();
|
315 | try {
|
316 | markup = this.performInitialMount(renderedElement, hostParent, hostContainerInfo, transaction, context);
|
317 | } catch (e) {
|
318 |
|
319 | transaction.rollback(checkpoint);
|
320 | this._instance.unstable_handleError(e);
|
321 | if (this._pendingStateQueue) {
|
322 | this._instance.state = this._processPendingState(this._instance.props, this._instance.context);
|
323 | }
|
324 | checkpoint = transaction.checkpoint();
|
325 |
|
326 | this._renderedComponent.unmountComponent(true);
|
327 | transaction.rollback(checkpoint);
|
328 |
|
329 |
|
330 |
|
331 | markup = this.performInitialMount(renderedElement, hostParent, hostContainerInfo, transaction, context);
|
332 | }
|
333 | return markup;
|
334 | },
|
335 |
|
336 | performInitialMount: function (renderedElement, hostParent, hostContainerInfo, transaction, context) {
|
337 | var inst = this._instance;
|
338 |
|
339 | var debugID = 0;
|
340 | if (process.env.NODE_ENV !== 'production') {
|
341 | debugID = this._debugID;
|
342 | }
|
343 |
|
344 | if (inst.componentWillMount) {
|
345 | if (process.env.NODE_ENV !== 'production') {
|
346 | measureLifeCyclePerf(function () {
|
347 | return inst.componentWillMount();
|
348 | }, debugID, 'componentWillMount');
|
349 | } else {
|
350 | inst.componentWillMount();
|
351 | }
|
352 |
|
353 |
|
354 | if (this._pendingStateQueue) {
|
355 | inst.state = this._processPendingState(inst.props, inst.context);
|
356 | }
|
357 | }
|
358 |
|
359 |
|
360 | if (renderedElement === undefined) {
|
361 | renderedElement = this._renderValidatedComponent();
|
362 | }
|
363 |
|
364 | var nodeType = ReactNodeTypes.getType(renderedElement);
|
365 | this._renderedNodeType = nodeType;
|
366 | var child = this._instantiateReactComponent(renderedElement, nodeType !== ReactNodeTypes.EMPTY
|
367 | );
|
368 | this._renderedComponent = child;
|
369 |
|
370 | var markup = ReactReconciler.mountComponent(child, transaction, hostParent, hostContainerInfo, this._processChildContext(context), debugID);
|
371 |
|
372 | if (process.env.NODE_ENV !== 'production') {
|
373 | if (debugID !== 0) {
|
374 | var childDebugIDs = child._debugID !== 0 ? [child._debugID] : [];
|
375 | ReactInstrumentation.debugTool.onSetChildren(debugID, childDebugIDs);
|
376 | }
|
377 | }
|
378 |
|
379 | return markup;
|
380 | },
|
381 |
|
382 | getHostNode: function () {
|
383 | return ReactReconciler.getHostNode(this._renderedComponent);
|
384 | },
|
385 |
|
386 | |
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | unmountComponent: function (safely) {
|
393 | if (!this._renderedComponent) {
|
394 | return;
|
395 | }
|
396 |
|
397 | var inst = this._instance;
|
398 |
|
399 | if (inst.componentWillUnmount && !inst._calledComponentWillUnmount) {
|
400 | inst._calledComponentWillUnmount = true;
|
401 |
|
402 | if (safely) {
|
403 | var name = this.getName() + '.componentWillUnmount()';
|
404 | ReactErrorUtils.invokeGuardedCallback(name, inst.componentWillUnmount.bind(inst));
|
405 | } else {
|
406 | if (process.env.NODE_ENV !== 'production') {
|
407 | measureLifeCyclePerf(function () {
|
408 | return inst.componentWillUnmount();
|
409 | }, this._debugID, 'componentWillUnmount');
|
410 | } else {
|
411 | inst.componentWillUnmount();
|
412 | }
|
413 | }
|
414 | }
|
415 |
|
416 | if (this._renderedComponent) {
|
417 | ReactReconciler.unmountComponent(this._renderedComponent, safely);
|
418 | this._renderedNodeType = null;
|
419 | this._renderedComponent = null;
|
420 | this._instance = null;
|
421 | }
|
422 |
|
423 |
|
424 |
|
425 |
|
426 | this._pendingStateQueue = null;
|
427 | this._pendingReplaceState = false;
|
428 | this._pendingForceUpdate = false;
|
429 | this._pendingCallbacks = null;
|
430 | this._pendingElement = null;
|
431 |
|
432 |
|
433 |
|
434 | this._context = null;
|
435 | this._rootNodeID = 0;
|
436 | this._topLevelWrapper = null;
|
437 |
|
438 |
|
439 |
|
440 |
|
441 | ReactInstanceMap.remove(inst);
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | },
|
449 |
|
450 | |
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | _maskContext: function (context) {
|
459 | var Component = this._currentElement.type;
|
460 | var contextTypes = Component.contextTypes;
|
461 | if (!contextTypes) {
|
462 | return emptyObject;
|
463 | }
|
464 | var maskedContext = {};
|
465 | for (var contextName in contextTypes) {
|
466 | maskedContext[contextName] = context[contextName];
|
467 | }
|
468 | return maskedContext;
|
469 | },
|
470 |
|
471 | |
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | _processContext: function (context) {
|
480 | var maskedContext = this._maskContext(context);
|
481 | if (process.env.NODE_ENV !== 'production') {
|
482 | var Component = this._currentElement.type;
|
483 | if (Component.contextTypes) {
|
484 | this._checkContextTypes(Component.contextTypes, maskedContext, 'context');
|
485 | }
|
486 | }
|
487 | return maskedContext;
|
488 | },
|
489 |
|
490 | |
491 |
|
492 |
|
493 |
|
494 |
|
495 | _processChildContext: function (currentContext) {
|
496 | var Component = this._currentElement.type;
|
497 | var inst = this._instance;
|
498 | var childContext;
|
499 |
|
500 | if (inst.getChildContext) {
|
501 | if (process.env.NODE_ENV !== 'production') {
|
502 | ReactInstrumentation.debugTool.onBeginProcessingChildContext();
|
503 | try {
|
504 | childContext = inst.getChildContext();
|
505 | } finally {
|
506 | ReactInstrumentation.debugTool.onEndProcessingChildContext();
|
507 | }
|
508 | } else {
|
509 | childContext = inst.getChildContext();
|
510 | }
|
511 | }
|
512 |
|
513 | if (childContext) {
|
514 | !(typeof Component.childContextTypes === 'object') ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s.getChildContext(): childContextTypes must be defined in order to use getChildContext().', this.getName() || 'ReactCompositeComponent') : _prodInvariant('107', this.getName() || 'ReactCompositeComponent') : void 0;
|
515 | if (process.env.NODE_ENV !== 'production') {
|
516 | this._checkContextTypes(Component.childContextTypes, childContext, 'child context');
|
517 | }
|
518 | for (var name in childContext) {
|
519 | !(name in Component.childContextTypes) ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s.getChildContext(): key "%s" is not defined in childContextTypes.', this.getName() || 'ReactCompositeComponent', name) : _prodInvariant('108', this.getName() || 'ReactCompositeComponent', name) : void 0;
|
520 | }
|
521 | return _assign({}, currentContext, childContext);
|
522 | }
|
523 | return currentContext;
|
524 | },
|
525 |
|
526 | |
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 | _checkContextTypes: function (typeSpecs, values, location) {
|
535 | if (process.env.NODE_ENV !== 'production') {
|
536 | checkReactTypeSpec(typeSpecs, values, location, this.getName(), null, this._debugID);
|
537 | }
|
538 | },
|
539 |
|
540 | receiveComponent: function (nextElement, transaction, nextContext) {
|
541 | var prevElement = this._currentElement;
|
542 | var prevContext = this._context;
|
543 |
|
544 | this._pendingElement = null;
|
545 |
|
546 | this.updateComponent(transaction, prevElement, nextElement, prevContext, nextContext);
|
547 | },
|
548 |
|
549 | |
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 | performUpdateIfNecessary: function (transaction) {
|
557 | if (this._pendingElement != null) {
|
558 | ReactReconciler.receiveComponent(this, this._pendingElement, transaction, this._context);
|
559 | } else if (this._pendingStateQueue !== null || this._pendingForceUpdate) {
|
560 | this.updateComponent(transaction, this._currentElement, this._currentElement, this._context, this._context);
|
561 | } else {
|
562 | this._updateBatchNumber = null;
|
563 | }
|
564 | },
|
565 |
|
566 | |
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 |
|
580 |
|
581 | updateComponent: function (transaction, prevParentElement, nextParentElement, prevUnmaskedContext, nextUnmaskedContext) {
|
582 | var inst = this._instance;
|
583 | !(inst != null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Attempted to update component `%s` that has already been unmounted (or failed to mount).', this.getName() || 'ReactCompositeComponent') : _prodInvariant('136', this.getName() || 'ReactCompositeComponent') : void 0;
|
584 |
|
585 | var willReceive = false;
|
586 | var nextContext;
|
587 |
|
588 |
|
589 | if (this._context === nextUnmaskedContext) {
|
590 | nextContext = inst.context;
|
591 | } else {
|
592 | nextContext = this._processContext(nextUnmaskedContext);
|
593 | willReceive = true;
|
594 | }
|
595 |
|
596 | var prevProps = prevParentElement.props;
|
597 | var nextProps = nextParentElement.props;
|
598 |
|
599 |
|
600 | if (prevParentElement !== nextParentElement) {
|
601 | willReceive = true;
|
602 | }
|
603 |
|
604 |
|
605 |
|
606 |
|
607 | if (willReceive && inst.componentWillReceiveProps) {
|
608 | if (process.env.NODE_ENV !== 'production') {
|
609 | measureLifeCyclePerf(function () {
|
610 | return inst.componentWillReceiveProps(nextProps, nextContext);
|
611 | }, this._debugID, 'componentWillReceiveProps');
|
612 | } else {
|
613 | inst.componentWillReceiveProps(nextProps, nextContext);
|
614 | }
|
615 | }
|
616 |
|
617 | var nextState = this._processPendingState(nextProps, nextContext);
|
618 | var shouldUpdate = true;
|
619 |
|
620 | if (!this._pendingForceUpdate) {
|
621 | if (inst.shouldComponentUpdate) {
|
622 | if (process.env.NODE_ENV !== 'production') {
|
623 | shouldUpdate = measureLifeCyclePerf(function () {
|
624 | return inst.shouldComponentUpdate(nextProps, nextState, nextContext);
|
625 | }, this._debugID, 'shouldComponentUpdate');
|
626 | } else {
|
627 | shouldUpdate = inst.shouldComponentUpdate(nextProps, nextState, nextContext);
|
628 | }
|
629 | } else {
|
630 | if (this._compositeType === CompositeTypes.PureClass) {
|
631 | shouldUpdate = !shallowEqual(prevProps, nextProps) || !shallowEqual(inst.state, nextState);
|
632 | }
|
633 | }
|
634 | }
|
635 |
|
636 | if (process.env.NODE_ENV !== 'production') {
|
637 | process.env.NODE_ENV !== 'production' ? warning(shouldUpdate !== undefined, '%s.shouldComponentUpdate(): Returned undefined instead of a ' + 'boolean value. Make sure to return true or false.', this.getName() || 'ReactCompositeComponent') : void 0;
|
638 | }
|
639 |
|
640 | this._updateBatchNumber = null;
|
641 | if (shouldUpdate) {
|
642 | this._pendingForceUpdate = false;
|
643 |
|
644 | this._performComponentUpdate(nextParentElement, nextProps, nextState, nextContext, transaction, nextUnmaskedContext);
|
645 | } else {
|
646 |
|
647 |
|
648 | this._currentElement = nextParentElement;
|
649 | this._context = nextUnmaskedContext;
|
650 | inst.props = nextProps;
|
651 | inst.state = nextState;
|
652 | inst.context = nextContext;
|
653 | }
|
654 | },
|
655 |
|
656 | _processPendingState: function (props, context) {
|
657 | var inst = this._instance;
|
658 | var queue = this._pendingStateQueue;
|
659 | var replace = this._pendingReplaceState;
|
660 | this._pendingReplaceState = false;
|
661 | this._pendingStateQueue = null;
|
662 |
|
663 | if (!queue) {
|
664 | return inst.state;
|
665 | }
|
666 |
|
667 | if (replace && queue.length === 1) {
|
668 | return queue[0];
|
669 | }
|
670 |
|
671 | var nextState = _assign({}, replace ? queue[0] : inst.state);
|
672 | for (var i = replace ? 1 : 0; i < queue.length; i++) {
|
673 | var partial = queue[i];
|
674 | _assign(nextState, typeof partial === 'function' ? partial.call(inst, nextState, props, context) : partial);
|
675 | }
|
676 |
|
677 | return nextState;
|
678 | },
|
679 |
|
680 | |
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 |
|
688 |
|
689 |
|
690 |
|
691 |
|
692 | _performComponentUpdate: function (nextElement, nextProps, nextState, nextContext, transaction, unmaskedContext) {
|
693 | var _this2 = this;
|
694 |
|
695 | var inst = this._instance;
|
696 |
|
697 | var hasComponentDidUpdate = Boolean(inst.componentDidUpdate);
|
698 | var prevProps;
|
699 | var prevState;
|
700 | var prevContext;
|
701 | if (hasComponentDidUpdate) {
|
702 | prevProps = inst.props;
|
703 | prevState = inst.state;
|
704 | prevContext = inst.context;
|
705 | }
|
706 |
|
707 | if (inst.componentWillUpdate) {
|
708 | if (process.env.NODE_ENV !== 'production') {
|
709 | measureLifeCyclePerf(function () {
|
710 | return inst.componentWillUpdate(nextProps, nextState, nextContext);
|
711 | }, this._debugID, 'componentWillUpdate');
|
712 | } else {
|
713 | inst.componentWillUpdate(nextProps, nextState, nextContext);
|
714 | }
|
715 | }
|
716 |
|
717 | this._currentElement = nextElement;
|
718 | this._context = unmaskedContext;
|
719 | inst.props = nextProps;
|
720 | inst.state = nextState;
|
721 | inst.context = nextContext;
|
722 |
|
723 | this._updateRenderedComponent(transaction, unmaskedContext);
|
724 |
|
725 | if (hasComponentDidUpdate) {
|
726 | if (process.env.NODE_ENV !== 'production') {
|
727 | transaction.getReactMountReady().enqueue(function () {
|
728 | measureLifeCyclePerf(inst.componentDidUpdate.bind(inst, prevProps, prevState, prevContext), _this2._debugID, 'componentDidUpdate');
|
729 | });
|
730 | } else {
|
731 | transaction.getReactMountReady().enqueue(inst.componentDidUpdate.bind(inst, prevProps, prevState, prevContext), inst);
|
732 | }
|
733 | }
|
734 | },
|
735 |
|
736 | |
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 | _updateRenderedComponent: function (transaction, context) {
|
743 | var prevComponentInstance = this._renderedComponent;
|
744 | var prevRenderedElement = prevComponentInstance._currentElement;
|
745 | var nextRenderedElement = this._renderValidatedComponent();
|
746 |
|
747 | var debugID = 0;
|
748 | if (process.env.NODE_ENV !== 'production') {
|
749 | debugID = this._debugID;
|
750 | }
|
751 |
|
752 | if (shouldUpdateReactComponent(prevRenderedElement, nextRenderedElement)) {
|
753 | ReactReconciler.receiveComponent(prevComponentInstance, nextRenderedElement, transaction, this._processChildContext(context));
|
754 | } else {
|
755 | var oldHostNode = ReactReconciler.getHostNode(prevComponentInstance);
|
756 | ReactReconciler.unmountComponent(prevComponentInstance, false);
|
757 |
|
758 | var nodeType = ReactNodeTypes.getType(nextRenderedElement);
|
759 | this._renderedNodeType = nodeType;
|
760 | var child = this._instantiateReactComponent(nextRenderedElement, nodeType !== ReactNodeTypes.EMPTY
|
761 | );
|
762 | this._renderedComponent = child;
|
763 |
|
764 | var nextMarkup = ReactReconciler.mountComponent(child, transaction, this._hostParent, this._hostContainerInfo, this._processChildContext(context), debugID);
|
765 |
|
766 | if (process.env.NODE_ENV !== 'production') {
|
767 | if (debugID !== 0) {
|
768 | var childDebugIDs = child._debugID !== 0 ? [child._debugID] : [];
|
769 | ReactInstrumentation.debugTool.onSetChildren(debugID, childDebugIDs);
|
770 | }
|
771 | }
|
772 |
|
773 | this._replaceNodeWithMarkup(oldHostNode, nextMarkup, prevComponentInstance);
|
774 | }
|
775 | },
|
776 |
|
777 | |
778 |
|
779 |
|
780 |
|
781 |
|
782 | _replaceNodeWithMarkup: function (oldHostNode, nextMarkup, prevInstance) {
|
783 | ReactComponentEnvironment.replaceNodeWithMarkup(oldHostNode, nextMarkup, prevInstance);
|
784 | },
|
785 |
|
786 | |
787 |
|
788 |
|
789 | _renderValidatedComponentWithoutOwnerOrContext: function () {
|
790 | var inst = this._instance;
|
791 | var renderedElement;
|
792 |
|
793 | if (process.env.NODE_ENV !== 'production') {
|
794 | renderedElement = measureLifeCyclePerf(function () {
|
795 | return inst.render();
|
796 | }, this._debugID, 'render');
|
797 | } else {
|
798 | renderedElement = inst.render();
|
799 | }
|
800 |
|
801 | if (process.env.NODE_ENV !== 'production') {
|
802 |
|
803 | if (renderedElement === undefined && inst.render._isMockFunction) {
|
804 |
|
805 |
|
806 | renderedElement = null;
|
807 | }
|
808 | }
|
809 |
|
810 | return renderedElement;
|
811 | },
|
812 |
|
813 | |
814 |
|
815 |
|
816 | _renderValidatedComponent: function () {
|
817 | var renderedElement;
|
818 | if (process.env.NODE_ENV !== 'production' || this._compositeType !== CompositeTypes.StatelessFunctional) {
|
819 | ReactCurrentOwner.current = this;
|
820 | try {
|
821 | renderedElement = this._renderValidatedComponentWithoutOwnerOrContext();
|
822 | } finally {
|
823 | ReactCurrentOwner.current = null;
|
824 | }
|
825 | } else {
|
826 | renderedElement = this._renderValidatedComponentWithoutOwnerOrContext();
|
827 | }
|
828 | !(
|
829 |
|
830 | renderedElement === null || renderedElement === false || React.isValidElement(renderedElement)) ? process.env.NODE_ENV !== 'production' ? invariant(false, '%s.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object.', this.getName() || 'ReactCompositeComponent') : _prodInvariant('109', this.getName() || 'ReactCompositeComponent') : void 0;
|
831 |
|
832 | return renderedElement;
|
833 | },
|
834 |
|
835 | |
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 | attachRef: function (ref, component) {
|
844 | var inst = this.getPublicInstance();
|
845 | !(inst != null) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Stateless function components cannot have refs.') : _prodInvariant('110') : void 0;
|
846 | var publicComponentInstance = component.getPublicInstance();
|
847 | if (process.env.NODE_ENV !== 'production') {
|
848 | var componentName = component && component.getName ? component.getName() : 'a component';
|
849 | process.env.NODE_ENV !== 'production' ? warning(publicComponentInstance != null || component._compositeType !== CompositeTypes.StatelessFunctional, 'Stateless function components cannot be given refs ' + '(See ref "%s" in %s created by %s). ' + 'Attempts to access this ref will fail.', ref, componentName, this.getName()) : void 0;
|
850 | }
|
851 | var refs = inst.refs === emptyObject ? inst.refs = {} : inst.refs;
|
852 | refs[ref] = publicComponentInstance;
|
853 | },
|
854 |
|
855 | |
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 |
|
862 | detachRef: function (ref) {
|
863 | var refs = this.getPublicInstance().refs;
|
864 | delete refs[ref];
|
865 | },
|
866 |
|
867 | |
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 | getName: function () {
|
874 | var type = this._currentElement.type;
|
875 | var constructor = this._instance && this._instance.constructor;
|
876 | return type.displayName || constructor && constructor.displayName || type.name || constructor && constructor.name || null;
|
877 | },
|
878 |
|
879 | /**
|
880 | * Get the publicly accessible representation of this component - i.e. what
|
881 | * is exposed by refs and returned by render. Can be null for stateless
|
882 | * components.
|
883 | *
|
884 | * @return {ReactComponent} the public component instance.
|
885 | * @internal
|
886 | */
|
887 | getPublicInstance: function () {
|
888 | var inst = this._instance;
|
889 | if (this._compositeType === CompositeTypes.StatelessFunctional) {
|
890 | return null;
|
891 | }
|
892 | return inst;
|
893 | },
|
894 |
|
895 |
|
896 | _instantiateReactComponent: null
|
897 | };
|
898 |
|
899 | module.exports = ReactCompositeComponent; |
\ | No newline at end of file |