简体   繁体   English

推送路由器时未捕获(承诺)DOMException

[英]Uncaught (in promise) DOMException when pushing router

I'm doing something with React, and I'm in a problem. 我在用React做某事,但遇到了问题。

Uncaught DOMException is occuring when pushing router. 推送路由器时发生未捕获的DOMException。

It occurs when I push a new URL into router with only an event triggered from a button in modal component, otherwise(not an event triggered from a modal) the router works well. 当我将新URL推送到路由器中时,仅发生了一个由模态组件中的按钮触发的事件,就会发生这种情况,否则(不是从模态中触发的事件)路由器运行良好。

Full procedure that I'd like to do is like below. 我想做的完整过程如下。

  1. There's a 'delete' button on the 'content view' page. “内容视图”页面上有一个“删除”按钮。
  2. If I click 'delete' button, 'confirm modal' appears. 如果单击“删除”按钮,则会显示“确认模式”。
  3. If I click 'confirm' button on 'confirm modal', the content is deleted and move to 'list view'. 如果单击“确认模式”上的“确认”按钮,内容将被删除并移至“列表视图”。

My Exception occurs right after deleting content. 删除内容后立即发生我的异常。

Belows are my source codes. 以下是我的源代码。

'delete' button: “删除”按钮:

<button key={keygen._()} className="btn btn-info btn-xs" style={{ marginRight: '4px' }} onClick={this._handleOnClickDel}>
        <i className="fa fa-eraser"></i> DELETE
</button>

'_handleOnClickDel' function: '_handleOnClickDel'函数:

_handleOnClickDel(e) {
  e.target.disabled = true;
  this.setState({
    showMessageModal: true, // this shows 'confirm modal'
  });
}

'confirm modal' component: “确认模式”部分:

<MessageModal
  message="Are you sure?"
  show={this.state.showMessageModal}
  cancelBtnTxt="CANCEL"
  confirmBtnTxt="CONFIRM"
  onConfirmClick={this._handleOnConfirmDelClick}
  close={this._hideMessageModal}
/>

'_handleOnConfirmDelClick' function: '_handleOnConfirmDelClick'函数:

_handleOnConfirmDelClick(e) {
  e.preventDefault();
  deleteById(this.props.params.boardId) // this deletes the content from DB
  .then(() => {
    this.context.router.push(`/community/${this.state.type}`);
  });
}

The full exception message on browser console is like below. 浏览器控制台上的完整异常消息如下所示。

Portal.js?8f33:75 Uncaught (in promise) DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. Portal.js?8f33:75未捕获(承诺)DOMException:无法在“节点”上执行“ removeChild”:要删除的节点不是该节点的子节点。 at Constructor._unmountOverlayTarget (eval at ( http://localhost:8000/dist/bundle.js:4284:2 ), :75:33) at Constructor.componentWillUnmount (eval at ( http://localhost:8000/dist/bundle.js:4284:2 ), :62:10) at ReactCompositeComponentWrapper.unmountComponent (eval at ( http://localhost:8000/dist/bundle.js:3138:2 ), :395:14) at Object.unmountComponent (eval at ( http://localhost:8000/dist/bundle.js:2790:2 ), :80:22) at ReactCompositeComponentWrapper.unmountComponent (eval at ( http://localhost:8000/dist/bundle.js:3138:2 ), :405:23) at Object.unmountComponent (eval at ( http://localhost:8000/dist/bundle.js:2790:2 ), :80:22) at ReactCompositeComponentWrapper.unmountComponent (eval at ( http://localhost:8000/dist/bundle.js:3138:2 ), :405:23) at Object.unmountComponent (eval at ( http://localhost:8000/dist/bundle.js:2790:2 ), :80:22) at Object.unmountChildren (eval at ( http://localhost:8000/dist/bundle.js:3126:2 ), :118:25) at ReactDOMComponent.unmountChildren (eval at ( http://localhost:8000 在Constructor._unmountOverlayTarget(在(EVAL HTTP://本地主机:8000 / DIST / bundle.js:4284:2 ),75:33)在Constructor.componentWillUnmount(在(EVAL HTTP://本地主机:8000 / DIST /在Object.unmountComponent处的ReactCompositeComponentWrapper.unmountComponent(eval在( http:// localhost:8000 / dist / bundle.js:3138:2 ),:395:14)处的bundle.js :4284:2 )::62:10) (eval位于( http:// localhost:8000 / dist / bundle.js:2790:2 )::80:22)at ReactCompositeComponentWrapper.unmountComponent(eval位于( http:// localhost:8000 / dist / bundle.js: 3138:2 ),:405:23)在React.CompositeComponentWrapper.unmountComponent(eval在( http:// localhost:8000 / dist / bundle.js:2790:2 ),:80:22)(在( http:// localhost:8000 / dist / bundle.js:3138:2 ):: 405:23)位于Object.unmountComponent(eval at( http:// localhost:8000 / dist / bundle.js:2790:2 ) ,:80:22)在Object.unmountChildren(eval在( http:// localhost:8000 / dist / bundle.js:3126:2 ),:118:25)在ReactDOMComponent.unmountChildren(eval在( http://本地主机:8000 /dist/bundle.js:3114:2 ), :346:28) /dist/bundle.js:3114:2),:346:28

Answer will be very appreciated. 答案将不胜感激。

You need to bind this explicitly on invoking _handleOnConfirmDelClick method from modal, so change it from: 您需要在从模式调用_handleOnConfirmDelClick方法时显式绑定this _handleOnConfirmDelClick ,因此请从以下方式进行更改:

onConfirmClick={this._handleOnConfirmDelClick}

to: 至:

onConfirmClick={this._handleOnConfirmDelClick.bind(this)}

and the method itself to be: 方法本身就是:

_handleOnConfirmDelClick(e, this) {
  var self = this;
  e.preventDefault();
  deleteById(this.props.params.boardId) // this deletes the content from DB
  .then(() => {
    self.context.router.push(`/community/${this.state.type}`);
  });
}

To read more about binding javascript this in ReactJS, see here . 要了解更多关于JavaScript的结合this在ReactJS,看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM