Example #1
0
function instantiateChild(childInstances, child, name, selfDebugID) {
  // We found a component instance.
  var keyUnique = childInstances[name] === undefined;
  if (__DEV__) {
    if (!ReactComponentTreeHook) {
      ReactComponentTreeHook = require('ReactGlobalSharedState')
        .ReactComponentTreeHook;
    }
    if (!keyUnique) {
      warning(
        false,
        'flattenChildren(...): ' +
          'Encountered two children with the same key, `%s`. ' +
          'Keys should be unique so that components maintain their identity ' +
          'across updates. Non-unique keys may cause children to be ' +
          'duplicated and/or omitted — the behavior is unsupported and ' +
          'could change in a future version.%s',
        KeyEscapeUtils.unescapeInDev(name),
        ReactComponentTreeHook.getStackAddendumByID(selfDebugID),
      );
    }
  }
  if (child != null && keyUnique) {
    childInstances[name] = instantiateReactComponent(child, true);
  }
}
Example #2
0
 mountChildren: function(nestedChildren, transaction, context) {
   invariant(context !== undefined, "Context is required parameter");
   var children = flattenChildren(nestedChildren);
   var mountImages = [];
   var index = 0;
   this._renderedChildren = children;
   for (var name in children) {
     var child = children[name];
     if (children.hasOwnProperty(name)) {
       // The rendered children must be turned into instances as they're
       // mounted.
       var childInstance = instantiateReactComponent(child, null);
       children[name] = childInstance;
       // Inlined for performance, see `ReactInstanceHandles.createReactID`.
       var rootID = this._rootNodeID + name;
       var mountImage = childInstance.mountComponent(
         rootID,
         transaction,
         this._mountDepth + 1,
         context
       );
       childInstance._mountIndex = index;
       mountImages.push(mountImage);
       index++;
     }
   }
   return mountImages;
 },
Example #3
0
 _updateRenderedComponent: function(transaction) {
   var prevComponentInstance = this._renderedComponent;
   var prevRenderedElement = prevComponentInstance._currentElement;
   var nextRenderedElement = this._renderValidatedComponent();
   if (shouldUpdateReactComponent(prevRenderedElement, nextRenderedElement)) {
     prevComponentInstance.receiveComponent(
       nextRenderedElement,
       transaction
     );
   } else {
     // These two IDs are actually the same! But nothing should rely on that.
     var thisID = this._rootNodeID;
     var prevComponentID = prevComponentInstance._rootNodeID;
     prevComponentInstance.unmountComponent();
     this._renderedComponent = instantiateReactComponent(
       nextRenderedElement,
       this._currentElement.type
     );
     var nextMarkup = this._renderedComponent.mountComponent(
       thisID,
       transaction,
       this._mountDepth + 1
     );
     ReactComponent.BackendIDOperations.dangerouslyReplaceNodeWithMarkupByID(
       prevComponentID,
       nextMarkup
     );
   }
 },
Example #4
0
    function(transaction, prevParentDescriptor) {
      ReactComponent.Mixin.updateComponent.call(
        this,
        transaction,
        prevParentDescriptor
      );

      var prevComponentInstance = this._renderedComponent;
      var prevDescriptor = prevComponentInstance._descriptor;
      var nextDescriptor = this._renderValidatedComponent();
      if (shouldUpdateReactComponent(prevDescriptor, nextDescriptor)) {
        prevComponentInstance.receiveComponent(nextDescriptor, transaction);
      } else {
        // These two IDs are actually the same! But nothing should rely on that.
        var thisID = this._rootNodeID;
        var prevComponentID = prevComponentInstance._rootNodeID;
        prevComponentInstance.unmountComponent();
        this._renderedComponent = instantiateReactComponent(nextDescriptor);
        var nextMarkup = this._renderedComponent.mountComponent(
          thisID,
          transaction,
          this._mountDepth + 1
        );
        ReactComponent.BackendIDOperations.dangerouslyReplaceNodeWithMarkupByID(
          prevComponentID,
          nextMarkup
        );
      }
    }
Example #5
0
 mountComponent: ReactPerf.measure('ReactCompositeComponent', 'mountComponent', function(rootID, transaction, mountDepth) {
   ReactComponent.Mixin.mountComponent.call(this, rootID, transaction, mountDepth);
   this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING;
   if (this.__reactAutoBindMap) {
     this._bindAutoBindMethods();
   }
   this.context = this._processContext(this._currentElement._context);
   this.props = this._processProps(this.props);
   this.state = this.getInitialState ? this.getInitialState() : null;
   invariant(typeof this.state === 'object' && !Array.isArray(this.state), '%s.getInitialState(): must return an object or null', this.constructor.displayName || 'ReactCompositeComponent');
   this._pendingState = null;
   this._pendingForceUpdate = false;
   if (this.componentWillMount) {
     this.componentWillMount();
     if (this._pendingState) {
       this.state = this._pendingState;
       this._pendingState = null;
     }
   }
   this._renderedComponent = instantiateReactComponent(this._renderValidatedComponent(), this._currentElement.type);
   this._compositeLifeCycleState = null;
   var markup = this._renderedComponent.mountComponent(rootID, transaction, mountDepth + 1);
   if (this.componentDidMount) {
     transaction.getReactMountReady().enqueue(this.componentDidMount, this);
   }
   return markup;
 }),
Example #6
0
    function(
        nextComponent,
        container,
        shouldReuseMarkup) {
      // Various parts of our code (such as ReactCompositeComponent's
      // _renderValidatedComponent) assume that calls to render aren't nested;
      // verify that that's the case.
      warning(
        ReactCurrentOwner.current == null,
        '_renderNewRootComponent(): Render methods should be a pure function ' +
        'of props and state; triggering nested component updates from ' +
        'render is not allowed. If necessary, trigger nested updates in ' +
        'componentDidUpdate.'
      );

      var componentInstance = instantiateReactComponent(nextComponent, null);
      var reactRootID = ReactMount._registerComponent(
        componentInstance,
        container
      );
      componentInstance.mountComponentIntoNode(
        reactRootID,
        container,
        shouldReuseMarkup
      );

      if (__DEV__) {
        // Record the root element in case it later gets transplanted.
        rootElementsByReactRootID[reactRootID] =
          getReactRootElementInContainer(container);
      }

      return componentInstance;
    }
Example #7
0
  _renderNewRootComponent: function(
    nextElement,
    container,
    shouldReuseMarkup,
    context
  ) {
    if (__DEV__) {
      ReactInstrumentation.debugTool.onBeginFlush();
    }

    // Various parts of our code (such as ReactCompositeComponent's
    // _renderValidatedComponent) assume that calls to render aren't nested;
    // verify that that's the case.
    warning(
      ReactCurrentOwner.current == null,
      '_renderNewRootComponent(): Render methods should be a pure function ' +
      'of props and state; triggering nested component updates from ' +
      'render is not allowed. If necessary, trigger nested updates in ' +
      'componentDidUpdate. Check the render method of %s.',
      ReactCurrentOwner.current && ReactCurrentOwner.current.getName() ||
        'ReactCompositeComponent'
    );

    invariant(
      container && (
        container.nodeType === ELEMENT_NODE_TYPE ||
        container.nodeType === DOC_NODE_TYPE ||
        container.nodeType === DOCUMENT_FRAGMENT_NODE_TYPE
      ),
      '_registerComponent(...): Target container is not a DOM element.'
    );

    ReactBrowserEventEmitter.ensureScrollValueMonitoring();
    var componentInstance = instantiateReactComponent(nextElement, false);

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      componentInstance,
      container,
      shouldReuseMarkup,
      context
    );

    var wrapperID = componentInstance._instance.rootID;
    instancesByReactRootID[wrapperID] = componentInstance;

    if (__DEV__) {
      // The instance here is TopLevelWrapper so we report mount for its child.
      ReactInstrumentation.debugTool.onMountRootComponent(
        componentInstance._renderedComponent._debugID
      );
      ReactInstrumentation.debugTool.onEndFlush();
    }

    return componentInstance;
  },
Example #8
0
    it('should render wrapped markup', function() {
      var markup = instantiateReactComponent(
        <th />
      ).mountComponent('.rX', transaction, 0);
      var output = Danger.dangerouslyRenderMarkup([markup])[0];

      expect(output.nodeName).toBe('TH');
    });
Example #9
0
  renderComponent: function(
    nextElement: ReactElement,
    containerTag: number,
    callback?: ?(() => void)
  ): ?ReactComponent {
    var topRootNodeID = ReactIOSTagHandles.tagToRootNodeID[containerTag];
    if (topRootNodeID) {
      var prevComponent = ReactIOSMount._instancesByContainerID[topRootNodeID];
      if (prevComponent) {
        var prevElement = prevComponent._currentElement;
        if (shouldUpdateReactComponent(prevElement, nextElement)) {
          ReactUpdateQueue.enqueueElementInternal(prevComponent, nextElement);
          if (callback) {
            ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
          }
          return prevComponent;
        } else {
          ReactIOSMount.unmountComponentAtNode(containerTag);
        }
      }
    }

    if (!ReactIOSTagHandles.reactTagIsNativeTopRootID(containerTag)) {
      console.error('You cannot render into anything but a top root');
      return;
    }

    var topRootNodeID = ReactIOSTagHandles.allocateRootNodeIDForTag(containerTag);
    ReactIOSTagHandles.associateRootNodeIDWithMountedNodeHandle(
      topRootNodeID,
      containerTag
    );

    var instance = instantiateReactComponent(nextElement);
    ReactIOSMount._instancesByContainerID[topRootNodeID] = instance;

    var childRootNodeID = instanceNumberToChildRootID(
      topRootNodeID,
      ReactIOSMount.instanceCount++
    );

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      instance,
      childRootNodeID,
      topRootNodeID
    );
    var component = instance.getPublicInstance();
    if (callback) {
      callback.call(component);
    }
    return component;
  },
Example #10
0
  renderComponent: function(
    nextElement: ReactElement,
    containerTag: number,
    callback?: ?(() => void)
  ): ?ReactComponent {
    var nextWrappedElement = new ReactElement(
      TopLevelWrapper,
      null,
      null,
      null,
      null,
      null,
      nextElement
    );

    var topRootNodeID = containerTag;
    var prevComponent = ReactNativeMount._instancesByContainerID[topRootNodeID];
    if (prevComponent) {
      var prevWrappedElement = prevComponent._currentElement;
      var prevElement = prevWrappedElement.props;
      if (shouldUpdateReactComponent(prevElement, nextElement)) {
        ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement);
        if (callback) {
          ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
        }
        return prevComponent;
      } else {
        ReactNativeMount.unmountComponentAtNode(containerTag);
      }
    }

    if (!ReactNativeTagHandles.reactTagIsNativeTopRootID(containerTag)) {
      console.error('You cannot render into anything but a top root');
      return null;
    }

    ReactNativeTagHandles.assertRootTag(containerTag);

    var instance = instantiateReactComponent(nextWrappedElement);
    ReactNativeMount._instancesByContainerID[containerTag] = instance;

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      instance,
      containerTag
    );
    var component = instance.getPublicInstance();
    if (callback) {
      callback.call(component);
    }
    return component;
  },
Example #11
0
  _renderNewRootComponent: function(
    nextElement,
    container,
    shouldReuseMarkup,
    context,
    callback,
  ) {
    // Various parts of our code (such as ReactCompositeComponent's
    // _renderValidatedComponent) assume that calls to render aren't nested;
    // verify that that's the case.
    warning(
      ReactCurrentOwner.current == null,
      '_renderNewRootComponent(): Render methods should be a pure function ' +
        'of props and state; triggering nested component updates from ' +
        'render is not allowed. If necessary, trigger nested updates in ' +
        'componentDidUpdate.\n\nCheck the render method of %s.',
      (ReactCurrentOwner.current && ReactCurrentOwner.current.getName()) ||
        'ReactCompositeComponent',
    );

    invariant(
      isValidContainer(container),
      '_registerComponent(...): Target container is not a DOM element.',
    );

    var componentInstance = instantiateReactComponent(nextElement, false);

    if (callback) {
      componentInstance._pendingCallbacks = [
        function() {
          validateCallback(callback);
          callback.call(
            componentInstance._renderedComponent.getPublicInstance(),
          );
        },
      ];
    }

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      componentInstance,
      container,
      shouldReuseMarkup,
      context,
    );

    var wrapperID = componentInstance._instance.rootID;
    instancesByReactRootID[wrapperID] = componentInstance;

    return componentInstance;
  },
Example #12
0
    function(rootID, transaction, mountDepth) {
      ReactComponent.Mixin.mountComponent.call(
        this,
        rootID,
        transaction,
        mountDepth
      );
      this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING;

      this.context = this._processContext(this._descriptor._context);
      this._defaultProps = this.getDefaultProps ? this.getDefaultProps() : null;
      this.props = this._processProps(this.props);

      if (this.__reactAutoBindMap) {
        this._bindAutoBindMethods();
      }

      this.state = this.getInitialState ? this.getInitialState() : null;
      invariant(
        typeof this.state === 'object' && !Array.isArray(this.state),
        '%s.getInitialState(): must return an object or null',
        this.constructor.displayName || 'ReactCompositeComponent'
      );

      this._pendingState = null;
      this._pendingForceUpdate = false;

      if (this.componentWillMount) {
        this.componentWillMount();
        // When mounting, calls to `setState` by `componentWillMount` will set
        // `this._pendingState` without triggering a re-render.
        if (this._pendingState) {
          this.state = this._pendingState;
          this._pendingState = null;
        }
      }

      this._renderedComponent = instantiateReactComponent(
        this._renderValidatedComponent()
      );

      // Done with mounting, `setState` will now trigger UI changes.
      this._compositeLifeCycleState = null;
      var markup = this._renderedComponent.mountComponent(
        rootID,
        transaction,
        mountDepth + 1
      );
      if (this.componentDidMount) {
        transaction.getReactMountReady().enqueue(this.componentDidMount, this);
      }
      return markup;
    }
 return transaction.perform(function() {
   var componentInstance = instantiateReactComponent(element);
   var markup = componentInstance.mountComponent(
     transaction,
     null,
     ReactDOMContainerInfo(),
     emptyObject
   );
   if (!makeStaticMarkup) {
     markup = ReactMarkupChecksum.addChecksumToMarkup(markup);
   }
   return markup;
 }, null);
Example #14
0
 instantiateChildren: function(nestedChildNodes, transaction, context) {
   var children = flattenChildren(nestedChildNodes);
   for (var name in children) {
     if (children.hasOwnProperty(name)) {
       var child = children[name];
       // The rendered children must be turned into instances as they're
       // mounted.
       var childInstance = instantiateReactComponent(child, null);
       children[name] = childInstance;
     }
   }
   return children;
 },
Example #15
0
    it('should render markup with props', function() {
      var markup = instantiateReactComponent(
        <div className="foo" />
      ).mountComponent(
        '.rX',
        transaction,
        0
      );
      var output = Danger.dangerouslyRenderMarkup([markup])[0];

      expect(output.nodeName).toBe('DIV');
      expect(output.className).toBe('foo');
    });
Example #16
0
 _updateChildren: function(nextNestedChildren, transaction, context) {
   invariant(context !== undefined, "Context is required parameter");
   var nextChildren = flattenChildren(nextNestedChildren);
   var prevChildren = this._renderedChildren;
   if (!nextChildren && !prevChildren) {
     return;
   }
   var name;
   // `nextIndex` will increment for each child in `nextChildren`, but
   // `lastIndex` will be the last index visited in `prevChildren`.
   var lastIndex = 0;
   var nextIndex = 0;
   for (name in nextChildren) {
     if (!nextChildren.hasOwnProperty(name)) {
       continue;
     }
     var prevChild = prevChildren && prevChildren[name];
     var prevElement = prevChild && prevChild._currentElement;
     var nextElement = nextChildren[name];
     if (shouldUpdateReactComponent(prevElement, nextElement)) {
       this.moveChild(prevChild, nextIndex, lastIndex);
       lastIndex = Math.max(prevChild._mountIndex, lastIndex);
       prevChild.receiveComponent(nextElement, transaction, context);
       prevChild._mountIndex = nextIndex;
     } else {
       if (prevChild) {
         // Update `lastIndex` before `_mountIndex` gets unset by unmounting.
         lastIndex = Math.max(prevChild._mountIndex, lastIndex);
         this._unmountChildByName(prevChild, name);
       }
       // The child must be instantiated before it's mounted.
       var nextChildInstance = instantiateReactComponent(
         nextElement,
         null
       );
       this._mountChildByNameAtIndex(
         nextChildInstance, name, nextIndex, transaction, context
       );
     }
     nextIndex++;
   }
   // Remove children that are no longer present.
   for (name in prevChildren) {
     if (prevChildren.hasOwnProperty(name) &&
         !(nextChildren && nextChildren.hasOwnProperty(name))) {
       this._unmountChildByName(prevChildren[name], name);
     }
   }
 },
Example #17
0
 updateChildren: function(
   prevChildren,
   nextNestedChildNodes,
   transaction,
   context) {
   // We currently don't have a way to track moves here but if we use iterators
   // instead of for..in we can zip the iterators and check if an item has
   // moved.
   // TODO: If nothing has changed, return the prevChildren object so that we
   // can quickly bailout if nothing has changed.
   var nextChildren = flattenChildren(nextNestedChildNodes);
   if (!nextChildren && !prevChildren) {
     return null;
   }
   var name;
   for (name in nextChildren) {
     if (!nextChildren.hasOwnProperty(name)) {
       continue;
     }
     var prevChild = prevChildren && prevChildren[name];
     var prevElement = prevChild && prevChild._currentElement;
     var nextElement = nextChildren[name];
     if (shouldUpdateReactComponent(prevElement, nextElement)) {
       ReactReconciler.receiveComponent(
         prevChild, nextElement, transaction, context
       );
       nextChildren[name] = prevChild;
     } else {
       if (prevChild) {
         ReactReconciler.unmountComponent(prevChild, name);
       }
       // The child must be instantiated before it's mounted.
       var nextChildInstance = instantiateReactComponent(
         nextElement,
         null
       );
       nextChildren[name] = nextChildInstance;
     }
   }
   // Unmount children that are no longer present.
   for (name in prevChildren) {
     if (prevChildren.hasOwnProperty(name) &&
         !(nextChildren && nextChildren.hasOwnProperty(name))) {
       ReactReconciler.unmountComponent(prevChildren[name]);
     }
   }
   return nextChildren;
 },
Example #18
0
function instantiateChild(childInstances, child, name) {
  // We found a component instance.
  var keyUnique = (childInstances[name] === undefined);
  if (__DEV__) {
    warning(
      keyUnique,
      'flattenChildren(...): Encountered two children with the same key, ' +
      '`%s`. Child keys must be unique; when two children share a key, only ' +
      'the first child will be used.',
      name
    );
  }
  if (child != null && keyUnique) {
    childInstances[name] = instantiateReactComponent(child, null);
  }
}
Example #19
0
 updateComponent: ReactPerf.measure('ReactCompositeComponent', 'updateComponent', function(transaction, prevParentElement) {
   ReactComponent.Mixin.updateComponent.call(this, transaction, prevParentElement);
   var prevComponentInstance = this._renderedComponent;
   var prevElement = prevComponentInstance._currentElement;
   var nextElement = this._renderValidatedComponent();
   if (shouldUpdateReactComponent(prevElement, nextElement)) {
     prevComponentInstance.receiveComponent(nextElement, transaction);
   } else {
     var thisID = this._rootNodeID;
     var prevComponentID = prevComponentInstance._rootNodeID;
     prevComponentInstance.unmountComponent();
     this._renderedComponent = instantiateReactComponent(nextElement, this._currentElement.type);
     var nextMarkup = this._renderedComponent.mountComponent(thisID, transaction, this._mountDepth + 1);
     ReactComponent.BackendIDOperations.dangerouslyReplaceNodeWithMarkupByID(prevComponentID, nextMarkup);
   }
 }),
Example #20
0
  _renderNewRootComponent: function(
    nextElement,
    container,
    shouldReuseMarkup,
    context
  ) {
    // Various parts of our code (such as ReactCompositeComponent's
    // _renderValidatedComponent) assume that calls to render aren't nested;
    // verify that that's the case.
    warning(
      ReactCurrentOwner.current == null,
      '_renderNewRootComponent(): Render methods should be a pure function ' +
      'of props and state; triggering nested component updates from ' +
      'render is not allowed. If necessary, trigger nested updates in ' +
      'componentDidUpdate. Check the render method of %s.',
      ReactCurrentOwner.current && ReactCurrentOwner.current.getName() ||
        'ReactCompositeComponent'
    );

    var componentInstance = instantiateReactComponent(nextElement, null);
    var reactRootID = ReactMount._registerComponent(
      componentInstance,
      container
    );

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      componentInstance,
      reactRootID,
      container,
      shouldReuseMarkup,
      context
    );

    if (__DEV__) {
      // Record the root element in case it later gets transplanted.
      rootElementsByReactRootID[reactRootID] =
        getReactRootElementInContainer(container);
    }

    return componentInstance;
  },
Example #21
0
  render: function(
    nextElement: ReactElement
  ): ?ReactElement<any, any, any> {
    var nextWrappedElement = new ReactElement(
      TopLevelWrapper,
      null,
      null,
      null,
      null,
      null,
      nextElement
    );

    // var prevComponent = ReactHostMount._instancesByContainerID[containerTag];
    // if (prevComponent) {
    //   var prevWrappedElement = prevComponent._currentElement;
    //   var prevElement = prevWrappedElement.props;
    //   if (shouldUpdateReactComponent(prevElement, nextElement)) {
    //     ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement);
    //     if (callback) {
    //       ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
    //     }
    //     return prevComponent;
    //   }
    // }

    var instance = instantiateReactComponent(nextWrappedElement, false);

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      instance
    );
    if (__DEV__) {
      // The instance here is TopLevelWrapper so we report mount for its child.
      ReactInstrumentation.debugTool.onMountRootComponent(
        instance._renderedComponent._debugID
      );
    }
    return new ReactTestInstance(instance);
  },
Example #22
0
 return transaction.perform(function() {
   var componentInstance = instantiateReactComponent(element);
   var markup = ReactReconciler.mountComponent(
     componentInstance,
     transaction,
     null,
     ReactDOMContainerInfo(),
     emptyObject
   );
   if (__DEV__) {
     ReactInstrumentation.debugTool.onUnmountComponent(
       componentInstance._debugID
     );
   }
   if (!makeStaticMarkup) {
     markup = ReactMarkupChecksum.addChecksumToMarkup(markup);
   }
   return markup;
 }, null);
Example #23
0
function instantiateChild(childInstances, child, name, selfDebugID) {
  // We found a component instance.
  var keyUnique = (childInstances[name] === undefined);
  if (__DEV__) {
    if (!ReactComponentTreeHook) {
      ReactComponentTreeHook = require('ReactComponentTreeHook');
    }
    warning(
      keyUnique,
      'flattenChildren(...): Encountered two children with the same key, ' +
      '`%s`. Child keys must be unique; when two children share a key, only ' +
      'the first child will be used.%s',
      KeyEscapeUtils.unescape(name),
      ReactComponentTreeHook.getStackAddendumByID(selfDebugID)
    );
  }
  if (child != null && keyUnique) {
    childInstances[name] = instantiateReactComponent(child, true);
  }
}
Example #24
0
  render: function(
    nextElement: ReactElement<any>,
    options?: TestRendererOptions,
  ): ReactTestInstance {
    var nextWrappedElement = React.createElement(
      TopLevelWrapper,
      {child: nextElement},
    );

    var instance = instantiateReactComponent(nextWrappedElement, false);

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.
    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      instance,
      Object.assign({}, defaultTestOptions, options),
    );
    return new ReactTestInstance(instance);
  },
Example #25
0
    function(
        nextComponent,
        container,
        shouldReuseMarkup) {

      var componentInstance = instantiateReactComponent(nextComponent);
      var reactRootID = ReactMount._registerComponent(
        componentInstance,
        container
      );
      componentInstance.mountComponentIntoNode(
        reactRootID,
        container,
        shouldReuseMarkup
      );

      if (__DEV__) {
        // Record the root element in case it later gets transplanted.
        rootElementsByReactRootID[reactRootID] =
          getReactRootElementInContainer(container);
      }

      return componentInstance;
    }
Example #26
0
    function(rootID, transaction, mountDepth) {
      ReactComponent.Mixin.mountComponent.call(
        this,
        rootID,
        transaction,
        mountDepth
      );
      this._compositeLifeCycleState = CompositeLifeCycle.MOUNTING;

      if (this.__reactAutoBindMap) {
        this._bindAutoBindMethods();
      }

      this.context = this._processContext(this._currentElement._context);
      this.props = this._processProps(this.props);

      this.state = this.getInitialState ? this.getInitialState() : null;

      if (__DEV__) {
        // We allow auto-mocks to proceed as if they're returning null.
        if (typeof this.state === 'undefined' &&
            this.getInitialState && this.getInitialState._isMockFunction) {
          // This is probably bad practice. Consider warning here and
          // deprecating this convenience.
          this.state = null;
        }
      }

      invariant(
        typeof this.state === 'object' && !Array.isArray(this.state),
        '%s.getInitialState(): must return an object or null',
        this.constructor.displayName || 'ReactCompositeComponent'
      );

      this._pendingState = null;
      this._pendingForceUpdate = false;

      if (this.componentWillMount) {
        this.componentWillMount();
        // When mounting, calls to `setState` by `componentWillMount` will set
        // `this._pendingState` without triggering a re-render.
        if (this._pendingState) {
          this.state = this._pendingState;
          this._pendingState = null;
        }
      }

      this._renderedComponent = instantiateReactComponent(
        this._renderValidatedComponent(),
        this._currentElement.type // The wrapping type
      );

      // Done with mounting, `setState` will now trigger UI changes.
      this._compositeLifeCycleState = null;
      var markup = this._renderedComponent.mountComponent(
        rootID,
        transaction,
        mountDepth + 1
      );
      if (this.componentDidMount) {
        transaction.getReactMountReady().enqueue(this.componentDidMount, this);
      }
      return markup;
    }
 updateChildren: function(
   prevChildren,
   nextChildren,
   mountImages,
   removedNodes,
   transaction,
   hostParent,
   hostContainerInfo,
   context,
   selfDebugID // 0 in production and for roots
 ) {
   // We currently don't have a way to track moves here but if we use iterators
   // instead of for..in we can zip the iterators and check if an item has
   // moved.
   // TODO: If nothing has changed, return the prevChildren object so that we
   // can quickly bailout if nothing has changed.
   if (!nextChildren && !prevChildren) {
     return;
   }
   var name;
   var prevChild;
   for (name in nextChildren) {
     if (!nextChildren.hasOwnProperty(name)) {
       continue;
     }
     prevChild = prevChildren && prevChildren[name];
     var prevElement = prevChild && prevChild._currentElement;
     var nextElement = nextChildren[name];
     if (prevChild != null &&
         shouldUpdateReactComponent(prevElement, nextElement)) {
       ReactReconciler.receiveComponent(
         prevChild, nextElement, transaction, context
       );
       nextChildren[name] = prevChild;
     } else {
       if (
         !ReactFeatureFlags.prepareNewChildrenBeforeUnmountInStack &&
         prevChild
       ) {
         removedNodes[name] = ReactReconciler.getHostNode(prevChild);
         ReactReconciler.unmountComponent(
           prevChild,
           false, /* safely */
           false /* skipLifecycle */
         );
       }
       // The child must be instantiated before it's mounted.
       var nextChildInstance = instantiateReactComponent(nextElement, true);
       nextChildren[name] = nextChildInstance;
       // Creating mount image now ensures refs are resolved in right order
       // (see https://github.com/facebook/react/pull/7101 for explanation).
       var nextChildMountImage = ReactReconciler.mountComponent(
         nextChildInstance,
         transaction,
         hostParent,
         hostContainerInfo,
         context,
         selfDebugID
       );
       mountImages.push(nextChildMountImage);
       if (
         ReactFeatureFlags.prepareNewChildrenBeforeUnmountInStack &&
         prevChild
       ) {
         removedNodes[name] = ReactReconciler.getHostNode(prevChild);
         ReactReconciler.unmountComponent(
           prevChild,
           false, /* safely */
           false /* skipLifecycle */
         );
       }
     }
   }
   // Unmount children that are no longer present.
   for (name in prevChildren) {
     if (prevChildren.hasOwnProperty(name) &&
         !(nextChildren && nextChildren.hasOwnProperty(name))) {
       prevChild = prevChildren[name];
       removedNodes[name] = ReactReconciler.getHostNode(prevChild);
       ReactReconciler.unmountComponent(
         prevChild,
         false, /* safely */
         false /* skipLifecycle */
       );
     }
   }
 },
Example #28
0
 return transaction.perform(function() {
   var componentInstance = instantiateReactComponent(element, null);
   var markup =
     componentInstance.mountComponent(id, transaction, emptyObject);
   return ReactMarkupChecksum.addChecksumToMarkup(markup);
 }, null);
Example #29
0
  renderComponent: function(
    nextElement: ReactElement,
    containerTag: number,
    callback?: ?(() => void)
  ): ?ReactComponent {
    var nextWrappedElement = new ReactElement(
      TopLevelWrapper,
      null,
      null,
      null,
      null,
      null,
      nextElement
    );

    var topRootNodeID = containerTag;
    var prevComponent = ReactNativeMount._instancesByContainerID[topRootNodeID];
    if (prevComponent) {
      var prevWrappedElement = prevComponent._currentElement;
      var prevElement = prevWrappedElement.props;
      if (shouldUpdateReactComponent(prevElement, nextElement)) {
        ReactUpdateQueue.enqueueElementInternal(prevComponent, nextWrappedElement);
        if (callback) {
          ReactUpdateQueue.enqueueCallbackInternal(prevComponent, callback);
        }
        return prevComponent;
      } else {
        ReactNativeMount.unmountComponentAtNode(containerTag);
      }
    }

    if (!ReactNativeTagHandles.reactTagIsNativeTopRootID(containerTag)) {
      console.error('You cannot render into anything but a top root');
      return null;
    }

    ReactNativeTagHandles.assertRootTag(containerTag);

    var instance = instantiateReactComponent(nextWrappedElement);
    ReactNativeMount._instancesByContainerID[containerTag] = instance;

    if (__DEV__) {
      // Mute future events from the top level wrapper.
      // It is an implementation detail that devtools should not know about.
      instance._debugID = 0;
    }

    // The initial render is synchronous but any updates that happen during
    // rendering, in componentWillMount or componentDidMount, will be batched
    // according to the current batching strategy.

    ReactUpdates.batchedUpdates(
      batchedMountComponentIntoNode,
      instance,
      containerTag
    );
    if (__DEV__) {
      // The instance here is TopLevelWrapper so we report mount for its child.
      ReactInstrumentation.debugTool.onMountRootComponent(
        instance._renderedComponent._debugID
      );
    }
    var component = instance.getPublicInstance();
    if (callback) {
      callback.call(component);
    }
    return component;
  },
Example #30
0
 return transaction.perform(function() {
   var componentInstance = instantiateReactComponent(element, null);
   return componentInstance.mountComponent(id, transaction, emptyObject);
 }, null);