-
Notifications
You must be signed in to change notification settings - Fork 49k
Pure Functions as Stateless Components #3995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2b7dce1
Pure Functions as Stateless Components
vslinko 232fab6
WIP
vslinko 0df2b5d
WIP: added failed test
vslinko 987082d
WIP: Test fix
vslinko 6e47c02
WIP: Lint fix
vslinko da77a5c
WIP: Remove attachRef and detachRef
vslinko 7cdcb39
WIP: Always should update
vslinko f46268f
WIP: Remove nextMountId and mountOrder
vslinko 5ff4f76
WIP: Merge _renderValidatedComponentWithoutOwnerOrContext into _rende…
vslinko ab3c531
WIP: Merge _replaceNodeWithMarkupByID into _updateRenderedComponent
vslinko a2088ea
WIP: Merge _performComponentUpdate into updateComponent
vslinko 463155e
WIP: Merge _updateRenderedComponent into updateComponent and remove t…
vslinko af96280
WIP: Remove ReactContext.current and ReactCurrentOwner.current from _…
vslinko 9a2d3b7
WIP: Remove unnecessary requires
vslinko dc4d156
WIP: Make lint happy
vslinko f821655
WIP: Hack: ReactStatelessOrCompositeComponentWrapper
vslinko 1cca595
WIP: Typo
vslinko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
262 changes: 262 additions & 0 deletions
262
src/renderers/shared/reconciler/ReactStatelessComponent.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
/** | ||
* Copyright 2013-2015, 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. | ||
* | ||
* @providesModule ReactStatelessComponent | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var ReactComponentEnvironment = require('ReactComponentEnvironment'); | ||
var ReactElement = require('ReactElement'); | ||
var ReactInstanceMap = require('ReactInstanceMap'); | ||
var ReactLifeCycle = require('ReactLifeCycle'); | ||
var ReactPerf = require('ReactPerf'); | ||
var ReactReconciler = require('ReactReconciler'); | ||
|
||
var emptyObject = require('emptyObject'); | ||
var invariant = require('invariant'); | ||
var shouldUpdateReactComponent = require('shouldUpdateReactComponent'); | ||
|
||
/** | ||
* @lends {ReactStatelessComponent.prototype} | ||
*/ | ||
var ReactStatelessComponentMixin = { | ||
/** | ||
* Initializes the component, renders markup, and registers event listeners. | ||
* | ||
* @param {string} rootID DOM ID of the root node. | ||
* @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction | ||
* @return {?string} Rendered markup to be inserted into the DOM. | ||
* @final | ||
* @internal | ||
*/ | ||
mountComponent: function(rootID, transaction, context) { | ||
this._rootNodeID = rootID; | ||
|
||
var publicProps = this._currentElement.props; | ||
|
||
// Initialize the public class | ||
var inst = {}; | ||
|
||
// These should be set up in the constructor, but as a convenience for | ||
// simpler class abstractions, we set them up after the fact. | ||
inst.props = publicProps; | ||
inst.refs = emptyObject; | ||
|
||
this._instance = inst; | ||
|
||
// Store a reference from the instance back to the internal representation | ||
ReactInstanceMap.set(inst, this); | ||
|
||
var renderedElement; | ||
|
||
var previouslyMounting = ReactLifeCycle.currentlyMountingInstance; | ||
ReactLifeCycle.currentlyMountingInstance = this; | ||
try { | ||
renderedElement = this._renderValidatedComponent(); | ||
} finally { | ||
ReactLifeCycle.currentlyMountingInstance = previouslyMounting; | ||
} | ||
|
||
this._renderedComponent = this._instantiateReactComponent( | ||
renderedElement, | ||
this._currentElement.type // The wrapping type | ||
); | ||
|
||
var markup = ReactReconciler.mountComponent( | ||
this._renderedComponent, | ||
rootID, | ||
transaction, | ||
context | ||
); | ||
|
||
return markup; | ||
}, | ||
|
||
/** | ||
* Releases any resources allocated by `mountComponent`. | ||
* | ||
* @final | ||
* @internal | ||
*/ | ||
unmountComponent: function() { | ||
var inst = this._instance; | ||
|
||
ReactReconciler.unmountComponent(this._renderedComponent); | ||
this._renderedComponent = null; | ||
|
||
// These fields do not really need to be reset since this object is no | ||
// longer accessible. | ||
this._rootNodeID = null; | ||
|
||
// Delete the reference from the instance to this internal representation | ||
// which allow the internals to be properly cleaned up even if the user | ||
// leaks a reference to the public instance. | ||
ReactInstanceMap.remove(inst); | ||
}, | ||
|
||
receiveComponent: function(nextElement, transaction, nextContext) { | ||
var prevElement = this._currentElement; | ||
|
||
this.updateComponent( | ||
transaction, | ||
prevElement, | ||
nextElement, | ||
nextContext | ||
); | ||
}, | ||
|
||
/** | ||
* @param {ReactReconcileTransaction} transaction | ||
* @internal | ||
*/ | ||
performUpdateIfNecessary: function(transaction) { | ||
}, | ||
|
||
/** | ||
* Perform an update to a mounted component. The componentWillReceiveProps and | ||
* shouldComponentUpdate methods are called, then (assuming the update isn't | ||
* skipped) the remaining update lifecycle methods are called and the DOM | ||
* representation is updated. | ||
* | ||
* By default, this implements React's rendering and reconciliation algorithm. | ||
* Sophisticated clients may wish to override this. | ||
* | ||
* @param {ReactReconcileTransaction} transaction | ||
* @param {ReactElement} prevParentElement | ||
* @param {ReactElement} nextParentElement | ||
* @internal | ||
* @overridable | ||
*/ | ||
updateComponent: function( | ||
transaction, | ||
prevParentElement, | ||
nextParentElement, | ||
nextContext | ||
) { | ||
var inst = this._instance; | ||
|
||
// Distinguish between a props update versus a simple state update | ||
if (prevParentElement !== nextParentElement) { | ||
inst.props = nextParentElement.props; | ||
} | ||
|
||
this._currentElement = nextParentElement; | ||
|
||
var prevComponentInstance = this._renderedComponent; | ||
var prevRenderedElement = prevComponentInstance._currentElement; | ||
var nextRenderedElement = this._renderValidatedComponent(); | ||
if (shouldUpdateReactComponent(prevRenderedElement, nextRenderedElement)) { | ||
ReactReconciler.receiveComponent( | ||
prevComponentInstance, | ||
nextRenderedElement, | ||
transaction, | ||
nextContext | ||
); | ||
} else { | ||
// These two IDs are actually the same! But nothing should rely on that. | ||
var thisID = this._rootNodeID; | ||
var prevComponentID = prevComponentInstance._rootNodeID; | ||
ReactReconciler.unmountComponent(prevComponentInstance); | ||
|
||
this._renderedComponent = this._instantiateReactComponent( | ||
nextRenderedElement, | ||
this._currentElement.type | ||
); | ||
var nextMarkup = ReactReconciler.mountComponent( | ||
this._renderedComponent, | ||
thisID, | ||
transaction, | ||
nextContext | ||
); | ||
ReactComponentEnvironment.replaceNodeWithMarkupByID( | ||
prevComponentID, | ||
nextMarkup | ||
); | ||
} | ||
}, | ||
|
||
/** | ||
* @private | ||
*/ | ||
_renderValidatedComponent: function() { | ||
var inst = this._instance; | ||
var render = this._currentElement.type; | ||
|
||
var renderedComponent = render(inst.props); | ||
|
||
if (__DEV__) { | ||
// We allow auto-mocks to proceed as if they're returning null. | ||
if (typeof renderedComponent === 'undefined' && | ||
render._isMockFunction) { | ||
// This is probably bad practice. Consider warning here and | ||
// deprecating this convenience. | ||
renderedComponent = null; | ||
} | ||
} | ||
|
||
invariant( | ||
// TODO: An `isValidNode` function would probably be more appropriate | ||
renderedComponent === null || renderedComponent === false || | ||
ReactElement.isValidElement(renderedComponent), | ||
'%s.render(): A valid ReactComponent must be returned. You may have ' + | ||
'returned undefined, an array or some other invalid object.', | ||
this.getName() || 'ReactStatelessComponent' | ||
); | ||
|
||
return renderedComponent; | ||
}, | ||
|
||
/** | ||
* Get a text description of the component that can be used to identify it | ||
* in error messages. | ||
* @return {string} The name or null. | ||
* @internal | ||
*/ | ||
getName: function() { | ||
var type = this._currentElement.type; | ||
|
||
return ( | ||
type.displayName || type.name || null | ||
); | ||
}, | ||
|
||
/** | ||
* Get the publicly accessible representation of this component - i.e. what | ||
* is exposed by refs and returned by React.render. Can be null for stateless | ||
* components. | ||
* | ||
* @return {ReactComponent} the public component instance. | ||
* @internal | ||
*/ | ||
getPublicInstance: function() { | ||
return this._instance; | ||
}, | ||
|
||
// Stub | ||
_instantiateReactComponent: null, | ||
|
||
}; | ||
|
||
ReactPerf.measureMethods( | ||
ReactStatelessComponentMixin, | ||
'ReactStatelessComponent', | ||
{ | ||
mountComponent: 'mountComponent', | ||
updateComponent: 'updateComponent', | ||
_renderValidatedComponent: '_renderValidatedComponent', | ||
} | ||
); | ||
|
||
var ReactStatelessComponent = { | ||
|
||
Mixin: ReactStatelessComponentMixin, | ||
|
||
}; | ||
|
||
module.exports = ReactStatelessComponent; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is always null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
React.findDOMNode
returns null if this method return null