为了账号安全,请及时绑定邮箱和手机立即绑定

阅读react源码时产生的疑问

阅读react源码时产生的疑问

慕侠2389804 2018-09-11 13:42:02
今天读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); }
查看完整描述

1 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

  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;

  },


查看完整回答
反对 回复 2018-10-19
  • 1 回答
  • 0 关注
  • 544 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信