@@ -60,6 +60,10 @@ function warnIfInvalidElement(Component, element) {
60
60
}
61
61
}
62
62
63
+ function shouldConstruct ( Component ) {
64
+ return Component . prototype && Component . prototype . isReactComponent ;
65
+ }
66
+
63
67
/**
64
68
* ------------------ The Life-Cycle of a Composite Component ------------------
65
69
*
@@ -158,44 +162,22 @@ var ReactCompositeComponentMixin = {
158
162
var Component = this . _currentElement . type ;
159
163
160
164
// Initialize the public class
161
- var inst ;
165
+ var inst = this . _constructComponent ( publicProps , publicContext ) ;
162
166
var renderedElement ;
163
167
164
- if ( Component . prototype && Component . prototype . isReactComponent ) {
165
- if ( __DEV__ ) {
166
- ReactCurrentOwner . current = this ;
167
- try {
168
- inst = new Component ( publicProps , publicContext , ReactUpdateQueue ) ;
169
- } finally {
170
- ReactCurrentOwner . current = null ;
171
- }
172
- } else {
173
- inst = new Component ( publicProps , publicContext , ReactUpdateQueue ) ;
174
- }
175
- } else {
176
- if ( __DEV__ ) {
177
- ReactCurrentOwner . current = this ;
178
- try {
179
- inst = Component ( publicProps , publicContext , ReactUpdateQueue ) ;
180
- } finally {
181
- ReactCurrentOwner . current = null ;
182
- }
183
- } else {
184
- inst = Component ( publicProps , publicContext , ReactUpdateQueue ) ;
185
- }
186
- if ( inst == null || inst . render == null ) {
187
- renderedElement = inst ;
188
- warnIfInvalidElement ( Component , renderedElement ) ;
189
- invariant (
190
- inst === null ||
191
- inst === false ||
192
- ReactElement . isValidElement ( inst ) ,
193
- '%s(...): A valid React element (or null) must be returned. You may have ' +
194
- 'returned undefined, an array or some other invalid object.' ,
195
- Component . displayName || Component . name || 'Component'
196
- ) ;
197
- inst = new StatelessComponent ( Component ) ;
198
- }
168
+ // Support functional components
169
+ if ( ! shouldConstruct ( Component ) && ( inst == null || inst . render == null ) ) {
170
+ renderedElement = inst ;
171
+ warnIfInvalidElement ( Component , renderedElement ) ;
172
+ invariant (
173
+ inst === null ||
174
+ inst === false ||
175
+ ReactElement . isValidElement ( inst ) ,
176
+ '%s(...): A valid React element (or null) must be returned. You may have ' +
177
+ 'returned undefined, an array or some other invalid object.' ,
178
+ Component . displayName || Component . name || 'Component'
179
+ ) ;
180
+ inst = new StatelessComponent ( Component ) ;
199
181
}
200
182
201
183
if ( __DEV__ ) {
@@ -323,6 +305,28 @@ var ReactCompositeComponentMixin = {
323
305
return markup ;
324
306
} ,
325
307
308
+ _constructComponent : function ( publicProps , publicContext ) {
309
+ if ( __DEV__ ) {
310
+ ReactCurrentOwner . current = this ;
311
+ try {
312
+ return this . _constructComponentWithoutOwner ( publicProps , publicContext ) ;
313
+ } finally {
314
+ ReactCurrentOwner . current = null ;
315
+ }
316
+ } else {
317
+ return this . _constructComponentWithoutOwner ( publicProps , publicContext ) ;
318
+ }
319
+ } ,
320
+
321
+ _constructComponentWithoutOwner : function ( publicProps , publicContext ) {
322
+ var Component = this . _currentElement . type ;
323
+ if ( shouldConstruct ( Component ) ) {
324
+ return new Component ( publicProps , publicContext , ReactUpdateQueue ) ;
325
+ } else {
326
+ return Component ( publicProps , publicContext , ReactUpdateQueue ) ;
327
+ }
328
+ } ,
329
+
326
330
performInitialMountWithErrorHandling : function (
327
331
renderedElement ,
328
332
nativeParent ,
0 commit comments