Javascript 尝试从react dom使用'unmountComponentAtNode',我得到了errpor`main.js:36 Uncaught(承诺中)`
我试图从DOM中删除一个元素,并尝试使用Javascript 尝试从react dom使用'unmountComponentAtNode',我得到了errpor`main.js:36 Uncaught(承诺中)`,javascript,reactjs,react-redux,react-router,Javascript,Reactjs,React Redux,React Router,我试图从DOM中删除一个元素,并尝试使用unmountComponentAtNode,因为我仍然在注册单击事件。 然而,当我试着跑的时候 handleClickOutside= async () =>{ console.log('the click is outside'); this.myRef = null; var element = await document.getElementsByClassName('RequestDropDown_contain
unmountComponentAtNode
,因为我仍然在注册单击事件。
然而,当我试着跑的时候
handleClickOutside= async () =>{
console.log('the click is outside');
this.myRef = null;
var element = await document.getElementsByClassName('RequestDropDown_container');
if (element.length == 0) {
return
}
element[0].parentNode.removeChild(element[0]);
unmountComponentAtNode(element[0]); //error is happening here
}
我在控制台中得到以下错误
main.js:36 Uncaught (in promise) Error: Minified React error #40; visit https://reactjs.org/docs/error-decoder.html?invariant=40 for the full message or use the non-minified dev environment for full errors and additional helpful warnings.
at t.unmountComponentAtNode (VM4083 main.js:36)
at ba.<anonymous> (VM4083 main.js:52)
at Generator.next (<anonymous>)
at n (VM4083 main.js:1)
at s (VM4083 main.js:1)
main.js:36未捕获(承诺中)错误:缩小反应错误#40;参观https://reactjs.org/docs/error-decoder.html?invariant=40 获取完整消息,或使用非小型开发环境获取完整错误和其他有用警告。
在t.unmountComponentAtNode(VM4083 main.js:36)
在英国航空公司。(VM4083 main.js:52)
在Generator.next()处
at n(VM4083 main.js:1)
在s(VM4083 main.js:1)
有什么我应该做的不同吗您是否单击了错误消息中的链接 main.js:36未捕获(承诺中)错误:缩小反应错误#40;访问获取完整消息,或使用非小型开发环境获取完整错误和其他有用警告 它说: unmountComponentAtNode(…):目标容器不是DOM元素 说明:
// First you remove the `element[0]` DOM node,
element[0].parentNode.removeChild(element[0]);
// then you tell ReactDOM to unmount component at node `element[0]`
unmountComponentAtNode(element[0]);
// which is already removed from DOM tree
// thus you get an error
解决方案,只需删除此行:
element[0].parentNode.removeChild(element[0]);
如何清理添加组件中的效果/侦听器?
componentWillMount(){document.addEventListener(“mousedown”,this.handleClick,false);}componentWillUnMount(){document.removeEventListener(“mousedown”,this.handleClick,false);}handleClick=(e)=>{if(this.myRef.contains(e.target)){console.log('your clicked inside')return;}this.handleClickOutside()}
@drewreseby您是否也将此handleClick
回调传递给任何子组件?不,仅在该组件@drewresei中使用了它,并且它工作正常,问题是,在运行unmountComponentAtNode(元素[0])之后,我仍然在该组件上触发事件侦听器@海猿