react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / core / instantiateReactComponent.js
81152 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule instantiateReactComponent9* @typechecks static-only10*/1112"use strict";1314var warning = require('warning');1516var ReactElement = require('ReactElement');17var ReactLegacyElement = require('ReactLegacyElement');18var ReactNativeComponent = require('ReactNativeComponent');19var ReactEmptyComponent = require('ReactEmptyComponent');2021/**22* Given an `element` create an instance that will actually be mounted.23*24* @param {object} element25* @param {*} parentCompositeType The composite type that resolved this.26* @return {object} A new instance of the element's constructor.27* @protected28*/29function instantiateReactComponent(element, parentCompositeType) {30var instance;3132if (__DEV__) {33warning(34element && (typeof element.type === 'function' ||35typeof element.type === 'string'),36'Only functions or strings can be mounted as React components.'37);3839// Resolve mock instances40if (element.type._mockedReactClassConstructor) {41// If this is a mocked class, we treat the legacy factory as if it was the42// class constructor for future proofing unit tests. Because this might43// be mocked as a legacy factory, we ignore any warnings triggerd by44// this temporary hack.45ReactLegacyElement._isLegacyCallWarningEnabled = false;46try {47instance = new element.type._mockedReactClassConstructor(48element.props49);50} finally {51ReactLegacyElement._isLegacyCallWarningEnabled = true;52}5354// If the mock implementation was a legacy factory, then it returns a55// element. We need to turn this into a real component instance.56if (ReactElement.isValidElement(instance)) {57instance = new instance.type(instance.props);58}5960var render = instance.render;61if (!render) {62// For auto-mocked factories, the prototype isn't shimmed and therefore63// there is no render function on the instance. We replace the whole64// component with an empty component instance instead.65element = ReactEmptyComponent.getEmptyComponent();66} else {67if (render._isMockFunction && !render._getMockImplementation()) {68// Auto-mocked components may have a prototype with a mocked render69// function. For those, we'll need to mock the result of the render70// since we consider undefined to be invalid results from render.71render.mockImplementation(72ReactEmptyComponent.getEmptyComponent73);74}75instance.construct(element);76return instance;77}78}79}8081// Special case string values82if (typeof element.type === 'string') {83instance = ReactNativeComponent.createInstanceForTag(84element.type,85element.props,86parentCompositeType87);88} else {89// Normal case for non-mocks and non-strings90instance = new element.type(element.props);91}9293if (__DEV__) {94warning(95typeof instance.construct === 'function' &&96typeof instance.mountComponent === 'function' &&97typeof instance.receiveComponent === 'function',98'Only React Components can be mounted.'99);100}101102// This actually sets up the internal instance. This will become decoupled103// from the public instance in a future diff.104instance.construct(element);105106return instance;107}108109module.exports = instantiateReactComponent;110111112