Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule instantiateReactComponent
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var warning = require('warning');
16
17
var ReactElement = require('ReactElement');
18
var ReactLegacyElement = require('ReactLegacyElement');
19
var ReactNativeComponent = require('ReactNativeComponent');
20
var ReactEmptyComponent = require('ReactEmptyComponent');
21
22
/**
23
* Given an `element` create an instance that will actually be mounted.
24
*
25
* @param {object} element
26
* @param {*} parentCompositeType The composite type that resolved this.
27
* @return {object} A new instance of the element's constructor.
28
* @protected
29
*/
30
function instantiateReactComponent(element, parentCompositeType) {
31
var instance;
32
33
if (__DEV__) {
34
warning(
35
element && (typeof element.type === 'function' ||
36
typeof element.type === 'string'),
37
'Only functions or strings can be mounted as React components.'
38
);
39
40
// Resolve mock instances
41
if (element.type._mockedReactClassConstructor) {
42
// If this is a mocked class, we treat the legacy factory as if it was the
43
// class constructor for future proofing unit tests. Because this might
44
// be mocked as a legacy factory, we ignore any warnings triggerd by
45
// this temporary hack.
46
ReactLegacyElement._isLegacyCallWarningEnabled = false;
47
try {
48
instance = new element.type._mockedReactClassConstructor(
49
element.props
50
);
51
} finally {
52
ReactLegacyElement._isLegacyCallWarningEnabled = true;
53
}
54
55
// If the mock implementation was a legacy factory, then it returns a
56
// element. We need to turn this into a real component instance.
57
if (ReactElement.isValidElement(instance)) {
58
instance = new instance.type(instance.props);
59
}
60
61
var render = instance.render;
62
if (!render) {
63
// For auto-mocked factories, the prototype isn't shimmed and therefore
64
// there is no render function on the instance. We replace the whole
65
// component with an empty component instance instead.
66
element = ReactEmptyComponent.getEmptyComponent();
67
} else {
68
if (render._isMockFunction && !render._getMockImplementation()) {
69
// Auto-mocked components may have a prototype with a mocked render
70
// function. For those, we'll need to mock the result of the render
71
// since we consider undefined to be invalid results from render.
72
render.mockImplementation(
73
ReactEmptyComponent.getEmptyComponent
74
);
75
}
76
instance.construct(element);
77
return instance;
78
}
79
}
80
}
81
82
// Special case string values
83
if (typeof element.type === 'string') {
84
instance = ReactNativeComponent.createInstanceForTag(
85
element.type,
86
element.props,
87
parentCompositeType
88
);
89
} else {
90
// Normal case for non-mocks and non-strings
91
instance = new element.type(element.props);
92
}
93
94
if (__DEV__) {
95
warning(
96
typeof instance.construct === 'function' &&
97
typeof instance.mountComponent === 'function' &&
98
typeof instance.receiveComponent === 'function',
99
'Only React Components can be mounted.'
100
);
101
}
102
103
// This actually sets up the internal instance. This will become decoupled
104
// from the public instance in a future diff.
105
instance.construct(element);
106
107
return instance;
108
}
109
110
module.exports = instantiateReactComponent;
111
112