diff --git a/src/isomorphic/classic/element/ReactElementValidator.js b/src/isomorphic/classic/element/ReactElementValidator.js index d4c2df2a07..2eb888ed08 100644 --- a/src/isomorphic/classic/element/ReactElementValidator.js +++ b/src/isomorphic/classic/element/ReactElementValidator.js @@ -23,7 +23,6 @@ var ReactFragment = require('ReactFragment'); var ReactPropTypeLocations = require('ReactPropTypeLocations'); var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames'); var ReactCurrentOwner = require('ReactCurrentOwner'); -var ReactNativeComponent = require('ReactNativeComponent'); var getIteratorFn = require('getIteratorFn'); var invariant = require('invariant'); @@ -293,18 +292,10 @@ function checkPropTypes(componentName, propTypes, props, location) { * @param {ReactElement} element */ function validatePropTypes(element) { - if (!(typeof element.type === 'string' || - typeof element.type === 'function')) { - // This has already warned. Don't throw. + var componentClass = element.type; + if (typeof componentClass !== 'function') { return; } - // Extract the component class from the element. Converts string types - // to a composite class which may have propTypes. - // TODO: Validating a string's propTypes is not decoupled from the - // rendering target which is problematic. - var componentClass = ReactNativeComponent.getComponentClassForElement( - element - ); var name = componentClass.displayName || componentClass.name; if (componentClass.propTypes) { checkPropTypes( diff --git a/src/renderers/dom/client/ReactMount.js b/src/renderers/dom/client/ReactMount.js index 724feae8c6..2b00a1580e 100644 --- a/src/renderers/dom/client/ReactMount.js +++ b/src/renderers/dom/client/ReactMount.js @@ -274,7 +274,7 @@ function mountComponentIntoNode( var markup = ReactReconciler.mountComponent( componentInstance, rootID, transaction, context ); - componentInstance._isTopLevel = true; + componentInstance._renderedComponent._topLevelWrapper = componentInstance; ReactMount._mountImageIntoNode(markup, container, shouldReuseMarkup); } @@ -328,6 +328,17 @@ function unmountComponentFromNode(instance, container) { } } +/** + * Temporary (?) hack so that we can store all top-level pending updates on + * composites instead of having to worry about different types of components + * here. + */ +var TopLevelWrapper = function() {}; +TopLevelWrapper.prototype.render = function() { + // this.props is actually a ReactElement + return this.props; +}; + /** * Mounting is the process of initializing a React component by creating its * representative DOM elements and inserting them into a supplied `container`. @@ -522,17 +533,26 @@ var ReactMount = { 'for your app.' ); + var nextWrappedElement = new ReactElement( + TopLevelWrapper, + null, + null, + null, + nextElement + ); + var prevComponent = instancesByReactRootID[getReactRootID(container)]; if (prevComponent) { - var prevElement = prevComponent._currentElement; + var prevWrappedElement = prevComponent._currentElement; + var prevElement = prevWrappedElement.props; if (shouldUpdateReactComponent(prevElement, nextElement)) { return ReactMount._updateRootComponent( prevComponent, - nextElement, + nextWrappedElement, container, callback - ).getPublicInstance(); + )._renderedComponent.getPublicInstance(); } else { ReactMount.unmountComponentAtNode(container); } @@ -563,7 +583,7 @@ var ReactMount = { var shouldReuseMarkup = containerHasReactMarkup && !prevComponent; var component = ReactMount._renderNewRootComponent( - nextElement, + nextWrappedElement, container, shouldReuseMarkup, parentComponent != null ? @@ -571,7 +591,7 @@ var ReactMount = { parentComponent._reactInternalInstance._context ) : emptyObject - ).getPublicInstance(); + )._renderedComponent.getPublicInstance(); if (callback) { callback.call(component); } diff --git a/src/renderers/dom/client/findDOMNode.js b/src/renderers/dom/client/findDOMNode.js index c8e3651047..64f6d5b2d1 100644 --- a/src/renderers/dom/client/findDOMNode.js +++ b/src/renderers/dom/client/findDOMNode.js @@ -17,7 +17,6 @@ var ReactInstanceMap = require('ReactInstanceMap'); var ReactMount = require('ReactMount'); var invariant = require('invariant'); -var isNode = require('isNode'); var warning = require('warning'); /** @@ -45,7 +44,7 @@ function findDOMNode(componentOrElement) { if (componentOrElement == null) { return null; } - if (isNode(componentOrElement)) { + if (componentOrElement.nodeType === 1) { return componentOrElement; } if (ReactInstanceMap.has(componentOrElement)) { diff --git a/src/renderers/dom/shared/ReactDOMComponent.js b/src/renderers/dom/shared/ReactDOMComponent.js index b960e05b0f..e4d7fe6bf1 100644 --- a/src/renderers/dom/shared/ReactDOMComponent.js +++ b/src/renderers/dom/shared/ReactDOMComponent.js @@ -30,6 +30,7 @@ var ReactDOMTextarea = require('ReactDOMTextarea'); var ReactMount = require('ReactMount'); var ReactMultiChild = require('ReactMultiChild'); var ReactPerf = require('ReactPerf'); +var ReactUpdateQueue = require('ReactUpdateQueue'); var assign = require('Object.assign'); var escapeTextContentForBrowser = require('escapeTextContentForBrowser'); @@ -51,6 +52,122 @@ var STYLE = keyOf({style: null}); var ELEMENT_NODE_TYPE = 1; +var canDefineProperty = false; +try { + Object.defineProperty({}, 'test', {get: function() {}}); + canDefineProperty = true; +} catch (e) { +} + +function getDeclarationErrorAddendum(internalInstance) { + if (internalInstance) { + var owner = internalInstance._currentElement._owner || null; + if (owner) { + var name = owner.getName(); + if (name) { + return ' This DOM node was rendered by `' + name + '`.'; + } + } + } + return ''; +} + +var legacyPropsDescriptor; +if (__DEV__) { + legacyPropsDescriptor = { + props: { + enumerable: false, + get: function() { + var component = this._reactInternalComponent; + warning( + false, + 'ReactDOMComponent: Do not access .props of a DOM node; instead, ' + + 'recreate the props as `render` did originally or read the DOM ' + + 'properties/attributes directly from this node (e.g., ' + + 'this.refs.box.className).%s', + getDeclarationErrorAddendum(component) + ); + return component._currentElement.props; + }, + }, + }; +} + +function legacyGetDOMNode() { + if (__DEV__) { + var component = this._reactInternalComponent; + warning( + false, + 'ReactDOMComponent: Do not access .getDOMNode() of a DOM node; ' + + 'instead, use the node directly.%s', + getDeclarationErrorAddendum(component) + ); + } + return this; +} + +function legacyIsMounted() { + var component = this._reactInternalComponent; + if (__DEV__) { + warning( + false, + 'ReactDOMComponent: Do not access .isMounted() of a DOM node.%s', + getDeclarationErrorAddendum(component) + ); + } + return !!component; +} + +function legacySetStateEtc() { + if (__DEV__) { + var component = this._reactInternalComponent; + warning( + false, + 'ReactDOMComponent: Do not access .setState(), .replaceState(), or ' + + '.forceUpdate() of a DOM node. This is a no-op.%s', + getDeclarationErrorAddendum(component) + ); + } +} + +function legacySetProps(partialProps, callback) { + var component = this._reactInternalComponent; + if (__DEV__) { + warning( + false, + 'ReactDOMComponent: Do not access .setProps() of a DOM node. ' + + 'Instead, call React.render again at the top level.%s', + getDeclarationErrorAddendum(component) + ); + } + if (!component) { + return; + } + ReactUpdateQueue.enqueueSetPropsInternal(component, partialProps); + if (callback) { + ReactUpdateQueue.enqueueCallbackInternal(component, callback); + } +} + +function legacyReplaceProps(partialProps, callback) { + var component = this._reactInternalComponent; + if (__DEV__) { + warning( + false, + 'ReactDOMComponent: Do not access .replaceProps() of a DOM node. ' + + 'Instead, call React.render again at the top level.%s', + getDeclarationErrorAddendum(component) + ); + } + if (!component) { + return; + } + ReactUpdateQueue.enqueueReplacePropsInternal(component, partialProps); + if (callback) { + ReactUpdateQueue.enqueueCallbackInternal(component, callback); + } +} + var styleMutationWarning = {}; function checkAndWarnForMutatedStyle(style1, style2, component) { @@ -327,6 +444,8 @@ function ReactDOMComponent(tag) { this._previousStyleCopy = null; this._rootNodeID = null; this._wrapperState = null; + this._topLevelWrapper = null; + this._nodeWithLegacyProperties = null; } ReactDOMComponent.displayName = 'ReactDOMComponent'; @@ -589,6 +708,10 @@ ReactDOMComponent.Mixin = { processChildContext(context, this) ); + if (!canDefineProperty && this._nodeWithLegacyProperties) { + this._nodeWithLegacyProperties.props = nextProps; + } + if (this._tag === 'select') { //