使用带有React + React路由器的getmdl抛出DOMException

时间:2017-04-07 01:22:39

标签: javascript reactjs react-router material-design-lite

这是一个基本示例,您可以看到source code hereaction here

请使用getmdl分支

 git clone -b getmdl https://gitlab.com/problems/react_router_mdl_js.git

当我在两个组件中使用mdl-js-layout并尝试从一个组件导航到另一个组件时,控制台中会抛出异常:

记住:抽屉应该工作

Error getmdl + react router

我尝试创建反应组件MdlLayout并调用upgradeDom

componentDidUpdate () {
  window.componentHandler.upgradeDom()
}

我尝试手动执行此操作

componentDidMount () {
  const layout = findDOMNode(this.refs.layout)
  window.componentHandler.upgradeElement(layout)
}

componentWillUnmount () {
  const layout = findDOMNode(this.refs.layout)
  window.componentHandler.downgradeElements(layout)
}

但没有任何作用。我阅读了几篇有关此内容的文章,例如this onethisthisthis

遵循例外

DOMChildrenOperations.js?568f:65 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
    at removeChild (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:503:1), <anonymous>:65:14)
    at Object.processUpdates (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:503:1), <anonymous>:209:11)
    at Object.dangerouslyProcessChildrenUpdates [as processChildrenUpdates] (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1550:1), <anonymous>:29:27)
    at processQueue (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1690:1), <anonymous>:137:29)
    at ReactDOMComponent._updateChildren (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1690:1), <anonymous>:355:9)
    at ReactDOMComponent.updateChildren (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1690:1), <anonymous>:299:12)
    at ReactDOMComponent._updateDOMChildren (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1522:1), <anonymous>:936:12)
    at ReactDOMComponent.updateComponent (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1522:1), <anonymous>:754:10)
    at ReactDOMComponent.receiveComponent (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:1522:1), <anonymous>:716:10)
    at Object.receiveComponent (eval at <anonymous> (https://reactroutermdljs-jlnjeksrvn.now.sh/main.js:286:1), <anonymous>:125:22)
removeChild @ DOMChildrenOperations.js?568f:65
processUpdates @ DOMChildrenOperations.js?568f:209
dangerouslyProcessChildrenUpdates @ ReactDOMIDOperations.js?2d83:29
processQueue @ ReactMultiChild.js?9682:137
_updateChildren @ ReactMultiChild.js?9682:355
updateChildren @ ReactMultiChild.js?9682:299
_updateDOMChildren @ ReactDOMComponent.js?ab8a:936
updateComponent @ ReactDOMComponent.js?ab8a:754
receiveComponent @ ReactDOMComponent.js?ab8a:716
receiveComponent @ ReactReconciler.js?399b:125
_updateRenderedComponent @ ReactCompositeComponent.js?d2b3:754
_performComponentUpdate @ ReactCompositeComponent.js?d2b3:724
updateComponent @ ReactCompositeComponent.js?d2b3:645
receiveComponent @ ReactCompositeComponent.js?d2b3:547
receiveComponent @ ReactReconciler.js?399b:125
_updateRenderedComponent @ ReactCompositeComponent.js?d2b3:754
_performComponentUpdate @ ReactCompositeComponent.js?d2b3:724
updateComponent @ ReactCompositeComponent.js?d2b3:645
receiveComponent @ ReactCompositeComponent.js?d2b3:547
receiveComponent @ ReactReconciler.js?399b:125
_updateRenderedComponent @ ReactCompositeComponent.js?d2b3:754
_performComponentUpdate @ ReactCompositeComponent.js?d2b3:724
updateComponent @ ReactCompositeComponent.js?d2b3:645
performUpdateIfNecessary @ ReactCompositeComponent.js?d2b3:561
performUpdateIfNecessary @ ReactReconciler.js?399b:157
runBatchedUpdates @ ReactUpdates.js?8e6b:150
perform @ Transaction.js?f15f:140
perform @ Transaction.js?f15f:140
perform @ ReactUpdates.js?8e6b:89
flushBatchedUpdates @ ReactUpdates.js?8e6b:172
closeAll @ Transaction.js?f15f:206
perform @ Transaction.js?f15f:153
batchedUpdates @ ReactDefaultBatchingStrategy.js?e9be:62
batchedUpdates @ ReactUpdates.js?8e6b:97
dispatchEvent @ ReactEventListener.js?944f:147

1 个答案:

答案 0 :(得分:2)

这里的问题是upgradeElementdowngradeElement API正在修改由React生成的DOM元素,导致它在重新审核阶段抛出异常,它会尝试替换view1路由组件with view2 route component(反之亦然)。您可以通过在MdlLayout.jsx中注释这些API调用来检查此理论,然后在切换路由时不会再出现错误。

任何尝试直接使用DOM的库都不能与React一起开箱即用。有关可能的解决方法,请阅读thisthis帖子。

更新

此处有一个项目fork,MDL调用已注释掉,从项目中检出temp分支并运行代码。路由器按预期工作。

至于DOM的操作,这里是没有MDL调用的DOM的屏幕截图 - enter image description here

这里有upgradeElement电话 - enter image description here

请注意,MDL库在未经React同意的情况下操作DOM。