阅读react源码时产生的疑问

今天读react源码有一个地方不太懂(第一次提问,多多指教)疑问在下方调用方法unmountComponent()处

// 卸载组件 

unmountComponent: function() { 

// 设置状态为 UNMOUNTING this._compositeLifeCycleState = CompositeLifeCycle.UNMOUNTING;

 // 如果存在 componentWillUnmount,则触发 

if (this.componentWillUnmount) { this.componentWillUnmount(); } 

// 更新状态为 null

 this._compositeLifeCycleState = null; 

//**下面调用unmountComponent()不会死循环吗**

this._renderedComponent.unmountComponent(); 

this._renderedComponent = null; ReactComponent.Mixin.unmountComponent.call(this); }



慕侠2389804
浏览 545回答 1
1回答

拉莫斯之舞

  unmountComponent: function (safely) {    if (!this._renderedComponent) {      return;    }    var inst = this._instance;    if (inst.componentWillUnmount && !inst._calledComponentWillUnmount) {      inst._calledComponentWillUnmount = true;      if (safely) {        var name = this.getName() + '.componentWillUnmount()';        ReactErrorUtils.invokeGuardedCallback(name, inst.componentWillUnmount.bind(inst));      } else {        if ("development" !== 'production') {          measureLifeCyclePerf(function () {            return inst.componentWillUnmount();          }, this._debugID, 'componentWillUnmount');        } else {          inst.componentWillUnmount();        }      }    }    if (this._renderedComponent) {      ReactReconciler.unmountComponent(this._renderedComponent, safely);      this._renderedNodeType = null;      this._renderedComponent = null;      this._instance = null;    }    // Reset pending fields    // Even if this component is scheduled for another update in ReactUpdates,    // it would still be ignored because these fields are reset.    this._pendingStateQueue = null;    this._pendingReplaceState = false;    this._pendingForceUpdate = false;    this._pendingCallbacks = null;    this._pendingElement = null;    // These fields do not really need to be reset since this object is no    // longer accessible.    this._context = null;    this._rootNodeID = 0;    this._topLevelWrapper = 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);    // Some existing components rely on inst.props even after they've been    // destroyed (in event handlers).    // TODO: inst.props = null;    // TODO: inst.state = null;    // TODO: inst.context = null;  },
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript