@samitouri / QOS-React-2 / commits / 11a7cb5b73
Fork 0

The library for web and native user interfaces.

Only Allow `forceUpdate` on Mounted Components

CommitSyncScript committed Jun 4, 2013 at 12:52 UTC 11a7cb5b734a1c28cce399fc8a56dbe43c7ce371
2 files changed +43
src/core/ReactCompositeComponent.js
+11
@@ -708,6 +708,17 @@ var ReactCompositeComponentMixin = {
708 * @protected
709 */
710 forceUpdate: function() {
711 + var compositeLifeCycleState = this._compositeLifeCycleState;
712 + invariant(
713 + this._lifeCycleState === ReactComponent.LifeCycle.MOUNTED,
714 + 'forceUpdate(...): Can only force an update on mounted components.'
715 + );
716 + invariant(
717 + compositeLifeCycleState !== CompositeLifeCycle.RECEIVING_STATE &&
718 + compositeLifeCycleState !== CompositeLifeCycle.UNMOUNTING,
719 + 'forceUpdate(...): Cannot force an update while unmounting component ' +
720 + 'or during an existing state transition (such as within `render`).'
721 + );
722 var transaction = ReactComponent.ReactReconcileTransaction.getPooled();
723 transaction.perform(
724 this._performComponentUpdate,
src/core/__tests__/ReactCompositeComponent-test.js
+32
@@ -270,4 +270,36 @@ describe('ReactCompositeComponent', function() {
270 }).not.toThrow();
271 });
272
273 + it('should not allow `forceUpdate` on unmounted components', function() {
274 + var container = document.createElement('div');
275 + document.documentElement.appendChild(container);
276 +
277 + var Component = React.createClass({
278 + render: function() {
279 + return <div />;
280 + }
281 + });
282 +
283 + var instance = <Component />;
284 + expect(function() {
285 + instance.forceUpdate();
286 + }).toThrow(
287 + 'Invariant Violation: forceUpdate(...): Can only force an update on ' +
288 + 'mounted components.'
289 + );
290 +
291 + React.renderComponent(instance, container);
292 + expect(function() {
293 + instance.forceUpdate();
294 + }).not.toThrow();
295 +
296 + React.unmountAndReleaseReactRootNode(container);
297 + expect(function() {
298 + instance.forceUpdate();
299 + }).toThrow(
300 + 'Invariant Violation: forceUpdate(...): Can only force an update on ' +
301 + 'mounted components.'
302 + );
303 + });
304 +
305 });