function inject() {
  ReactGenericBatching.injection.injectStackBatchedUpdates(
    ReactUpdates.batchedUpdates
  );

  ReactUpdates.injection.injectReconcileTransaction(
    ReactNativeComponentEnvironment.ReactReconcileTransaction
  );

  ReactUpdates.injection.injectBatchingStrategy(
    ReactDefaultBatchingStrategy
  );

  ReactComponentEnvironment.injection.injectEnvironment(
    ReactNativeComponentEnvironment
  );

  var EmptyComponent = (instantiate) => {
    // Can't import View at the top because it depends on React to make its composite
    var View = require('View');
    return new ReactSimpleEmptyComponent(
      React.createElement(View, {
        collapsable: true,
        style: { position: 'absolute' },
      }),
      instantiate
    );
  };

  findNodeHandle.injection.injectFindNode(
    (instance) => instance.getHostNode()
  );
  findNodeHandle.injection.injectFindRootNodeID(
    (instance) => instance._rootNodeID
  );

  ReactEmptyComponent.injection.injectEmptyComponentFactory(EmptyComponent);

  ReactHostComponent.injection.injectTextComponentClass(
    ReactNativeTextComponent
  );
  ReactHostComponent.injection.injectGenericComponentClass(function(tag) {
    // Show a nicer error message for non-function tags
    var info = '';
    if (typeof tag === 'string' && /^[a-z]/.test(tag)) {
      info += ' Each component name should start with an uppercase letter.';
    }
    invariant(false, 'Expected a component class, got %s.%s', tag, info);
  });
}
Beispiel #2
0
var findNodeHandle = require('findNodeHandle');

ReactNativeInjection.inject();
ReactNativeStackInjection.inject();

var render = function(
  element: ReactElement<any>,
  mountInto: number,
  callback?: ?(() => void)
): ?ReactComponent<any, any, any> {
  return ReactNativeMount.renderComponent(element, mountInto, callback);
};

findNodeHandle.injection.injectFindNode(
  (instance) => instance.getHostNode()
);
findNodeHandle.injection.injectFindRootNodeID(
  (instance) => instance._rootNodeID
);

var ReactNative = {
  hasReactNativeInitialized: false,
  findNodeHandle: findNodeHandle,
  render: render,
  unmountComponentAtNode: ReactNativeMount.unmountComponentAtNode,

  /* eslint-disable camelcase */
  unstable_batchedUpdates: ReactUpdates.batchedUpdates,
  /* eslint-enable camelcase */
    // No additional native views are created though.
    // It's not clear to me which is better so I'm deferring for now.
    // More context @ github.com/facebook/react/pull/8560#discussion_r92111303
    return false;
  },

  useSyncScheduling: true,
});

ReactGenericBatching.injection.injectFiberBatchedUpdates(
  NativeRenderer.batchedUpdates,
);

const roots = new Map();

findNodeHandle.injection.injectFindNode((fiber: Fiber) =>
  NativeRenderer.findHostInstance(fiber));
findNodeHandle.injection.injectFindRootNodeID(instance => instance);


// Intercept lifecycle errors and ensure they are shown with the correct stack
// trace within the native redbox component.
ReactFiberErrorLogger.injection.injectDialog(
  ReactNativeFiberErrorDialog.showDialog,
);

const ReactNative = {
  // External users of findNodeHandle() expect the host tag number return type.
  // The injected findNodeHandle() strategy returns the instance wrapper though.
  // See NativeMethodsMixin#setNativeProps for more info on why this is done.
  findNodeHandle(componentOrHandle: any): ?number {
    const instance: any = findNodeHandle(componentOrHandle);
    // More context @ github.com/facebook/react/pull/8560#discussion_r92111303
    return false;
  },

  useSyncScheduling: true,
});

ReactGenericBatching.injection.injectFiberBatchedUpdates(
  NativeRenderer.batchedUpdates
);

const roots = new Map();

findNodeHandle.injection.injectFindNode(
  (fiber: Fiber) => {
    const instance: any = NativeRenderer.findHostInstance(fiber);
    return instance ? instance._nativeTag : null;
  }
);
findNodeHandle.injection.injectFindRootNodeID(
  (instance) => instance._nativeTag
);

const ReactNative = {
  findNodeHandle,

  render(element : Element<any>, containerTag : any, callback: ?Function) {
    let root = roots.get(containerTag);

    if (!root) {
      // TODO (bvaughn): If we decide to keep the wrapper component,
      // We could create a wrapper for containerTag as well to reduce special casing.