react-native/Libraries/ReactNative/ReactNative.js

138 lines
4.3 KiB
JavaScript
Raw Normal View History

2015-01-30 01:10:49 +00:00
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
2015-01-30 01:10:49 +00:00
*
* @providesModule ReactNative
* @flow
2015-01-30 01:10:49 +00:00
*/
'use strict';
2015-01-30 01:10:49 +00:00
// Require ReactNativeDefaultInjection first for its side effects of setting up
// the JS environment
var ReactNativeDefaultInjection = require('ReactNativeDefaultInjection');
var ReactChildren = require('ReactChildren');
var ReactClass = require('ReactClass');
2015-01-30 01:10:49 +00:00
var ReactComponent = require('ReactComponent');
var ReactCurrentOwner = require('ReactCurrentOwner');
var ReactElement = require('ReactElement');
var ReactElementValidator = require('ReactElementValidator');
var ReactInstanceHandles = require('ReactInstanceHandles');
var ReactNativeMount = require('ReactNativeMount');
2015-01-30 01:10:49 +00:00
var ReactPropTypes = require('ReactPropTypes');
var ReactUpdates = require('ReactUpdates');
2015-01-30 01:10:49 +00:00
var findNodeHandle = require('findNodeHandle');
2015-01-30 01:10:49 +00:00
var invariant = require('invariant');
var onlyChild = require('onlyChild');
var warning = require('warning');
2015-01-30 01:10:49 +00:00
ReactNativeDefaultInjection.inject();
2015-01-30 01:10:49 +00:00
var createElement = ReactElement.createElement;
var createFactory = ReactElement.createFactory;
var cloneElement = ReactElement.cloneElement;
2015-01-30 01:10:49 +00:00
if (__DEV__) {
createElement = ReactElementValidator.createElement;
createFactory = ReactElementValidator.createFactory;
cloneElement = ReactElementValidator.cloneElement;
2015-01-30 01:10:49 +00:00
}
var resolveDefaultProps = function(element) {
// Could be optimized, but not currently in heavy use.
var defaultProps = element.type.defaultProps;
var props = element.props;
for (var propName in defaultProps) {
if (props[propName] === undefined) {
props[propName] = defaultProps[propName];
}
}
};
// Experimental optimized element creation
2015-07-30 03:19:40 +00:00
var augmentElement = function(element: ReactElement): ReactElement {
2015-01-30 01:10:49 +00:00
if (__DEV__) {
invariant(
false,
'This optimized path should never be used in DEV mode because ' +
'it does not provide validation. Check your JSX transform.'
);
}
element._owner = ReactCurrentOwner.current;
if (element.type.defaultProps) {
resolveDefaultProps(element);
}
return element;
};
var render = function(
element: ReactElement,
mountInto: number,
callback?: ?(() => void)
): ?ReactComponent {
return ReactNativeMount.renderComponent(element, mountInto, callback);
2015-01-30 01:10:49 +00:00
};
var ReactNative = {
hasReactNativeInitialized: false,
Children: {
map: ReactChildren.map,
forEach: ReactChildren.forEach,
count: ReactChildren.count,
only: onlyChild
},
Component: ReactComponent,
2015-01-30 01:10:49 +00:00
PropTypes: ReactPropTypes,
createClass: ReactClass.createClass,
2015-01-30 01:10:49 +00:00
createElement: createElement,
createFactory: createFactory,
cloneElement: cloneElement,
2015-01-30 01:10:49 +00:00
_augmentElement: augmentElement,
findNodeHandle: findNodeHandle,
2015-01-30 01:10:49 +00:00
render: render,
unmountComponentAtNode: ReactNativeMount.unmountComponentAtNode,
2015-01-30 01:10:49 +00:00
/* eslint-disable camelcase */
unstable_batchedUpdates: ReactUpdates.batchedUpdates,
/* eslint-enable camelcase */
// Hook for JSX spread, don't use this for anything else.
2015-01-30 01:10:49 +00:00
__spread: Object.assign,
unmountComponentAtNodeAndRemoveContainer: ReactNativeMount.unmountComponentAtNodeAndRemoveContainer,
2015-01-30 01:10:49 +00:00
isValidClass: ReactElement.isValidFactory,
isValidElement: ReactElement.isValidElement,
// Deprecations (remove for 0.13)
renderComponent: function(
element: ReactElement,
mountInto: number,
callback?: ?(() => void)
): ?ReactComponent {
warning('Use React.render instead of React.renderComponent');
return ReactNative.render(element, mountInto, callback);
},
2015-01-30 01:10:49 +00:00
};
// Inject the runtime into a devtools global hook regardless of browser.
// Allows for debugging when the hook is injected on the page.
/* globals __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined' &&
typeof __REACT_DEVTOOLS_GLOBAL_HOOK__.inject === 'function') {
__REACT_DEVTOOLS_GLOBAL_HOOK__.inject({
CurrentOwner: ReactCurrentOwner,
InstanceHandles: ReactInstanceHandles,
Mount: ReactNativeMount,
Reconciler: require('ReactReconciler'),
TextComponent: require('ReactNativeTextComponent'),
});
}
module.exports = ReactNative;