-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
Introducing ReactComponentBase base class #2805
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* Copyright 2013-2014, 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 ReactComponentBase | ||
*/ | ||
|
||
"use strict"; | ||
|
||
var ReactInstanceMap = require('ReactInstanceMap'); | ||
|
||
var assign = require('Object.assign'); | ||
var invariant = require('invariant'); | ||
var warning = require('warning'); | ||
|
||
/** | ||
* Base class helpers for the updating state of a component. | ||
*/ | ||
function ReactComponentBase(props) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we actually use ES6 classes in our src? It gets compiled down to this and the It's not a big deal but could be nice to start doing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully trust the transpilers that we use to be efficient since classes have to deal with other edge cases. For example, we might start including a runtime in the transpiler. They may also not always be spec compliant. It might not be equivalent since class methods are probably going to become non-enumerable which we can't do in IE8. But that has gone back and forth. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair. Not a big deal. We've gone the prototype route in the past too. |
||
this.props = props; | ||
} | ||
|
||
/** | ||
* Sets a subset of the state. Always use this to mutate | ||
* state. You should treat `this.state` as immutable. | ||
* | ||
* There is no guarantee that `this.state` will be immediately updated, so | ||
* accessing `this.state` after calling this method may return the old value. | ||
* | ||
* There is no guarantee that calls to `setState` will run synchronously, | ||
* as they may eventually be batched together. You can provide an optional | ||
* callback that will be executed when the call to setState is actually | ||
* completed. | ||
* | ||
* @param {object} partialState Next partial state to be merged with state. | ||
* @param {?function} callback Called after state is updated. | ||
* @final | ||
* @protected | ||
*/ | ||
ReactComponentBase.prototype.setState = function(partialState, callback) { | ||
invariant( | ||
typeof partialState === 'object' || partialState == null, | ||
'setState(...): takes an object of state variables to update.' | ||
); | ||
if (__DEV__) { | ||
warning( | ||
partialState != null, | ||
'setState(...): You passed an undefined or null state object; ' + | ||
'instead, use forceUpdate().' | ||
); | ||
} | ||
|
||
var internalInstance = ReactInstanceMap.get(this); | ||
invariant( | ||
internalInstance, | ||
'setState(...): Can only update a mounted or mounting component. ' + | ||
'This usually means you called setState() on an unmounted ' + | ||
'component.' | ||
); | ||
internalInstance.setState( | ||
partialState, callback && callback.bind(this) | ||
); | ||
}; | ||
|
||
/** | ||
* Forces an update. This should only be invoked when it is known with | ||
* certainty that we are **not** in a DOM transaction. | ||
* | ||
* You may want to call this when you know that some deeper aspect of the | ||
* component's state has changed but `setState` was not called. | ||
* | ||
* This will not invoke `shouldUpdateComponent`, but it will invoke | ||
* `componentWillUpdate` and `componentDidUpdate`. | ||
* | ||
* @param {?function} callback Called after update is complete. | ||
* @final | ||
* @protected | ||
*/ | ||
ReactComponentBase.prototype.forceUpdate = function(callback) { | ||
var internalInstance = ReactInstanceMap.get(this); | ||
invariant( | ||
internalInstance, | ||
'forceUpdate(...): Can only force an update on mounted or mounting ' + | ||
'components. This usually means you called forceUpdate() on an ' + | ||
'unmounted component.' | ||
); | ||
internalInstance.forceUpdate(callback && callback.bind(this)); | ||
}; | ||
|
||
/** | ||
* Deprecated APIs. These APIs used to exist on classic React classes but since | ||
* we would like to deprecate them, we're not going to move them over to this | ||
* modern base class. Instead, we define a getter that warns if it's accessed. | ||
*/ | ||
if (__DEV__) { | ||
if (Object.defineProperty) { | ||
var deprecatedAPIs = { | ||
getDOMNode: 'getDOMNode', | ||
isMounted: 'isMounted', | ||
replaceState: 'replaceState', | ||
setProps: 'setProps' | ||
}; | ||
var defineDeprecationWarning = function(methodName, displayName) { | ||
Object.defineProperty(ReactComponentBase.prototype, methodName, { | ||
get: function() { | ||
warning( | ||
false, | ||
'%s(...) is deprecated in plain JavaScript React classes.', | ||
displayName | ||
); | ||
return undefined; | ||
} | ||
}); | ||
}; | ||
for (var methodName in deprecatedAPIs) { | ||
if (deprecatedAPIs.hasOwnProperty(methodName)) { | ||
defineDeprecationWarning(methodName, deprecatedAPIs[methodName]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = ReactComponentBase; |
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.
We don't actually unmount in most of our tests. I think you could skip this, as well as appending container to the DOM (non of our tests do this, just having the node is good enough)
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 test is testing componentWillUnmount so I need to unmount it to trigger the expect in the test case.
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.
Ah, sorry, I missed that.