卸载React.js节点
我正在尝试使用卸载React.js节点this._rootNodeID
handleClick: function() {
React.unmountComponentAtNode(this._rootNodeID)
}
但它返回 false
。
单击元素时会触发 handleClick
,并应卸载根节点。有关 unmountComponentAtNode
此处的文档
The handleClick
is fired when I click on an element, and should unmount the root-node. Documentation on unmountComponentAtNode
here
我也试过这个:
React .unmountComponentAtNode($('* [data-reactid ='+ this._rootNodeID +']')[0])
React.unmountComponentAtNode($('*[data-reactid="'+this._rootNodeID+'"]')[0])
该选择器与 jQuery.hide()
一起使用,但不能与卸载它一起使用,而文档说它应该是 DOMElement
,就像你用于 React.renderComponent
That selector works with jQuery.hide()
, but not with unmounting it, while the documentation states it should be a DOMElement
, like you would use for React.renderComponent
经过几次测试后结果证明它适用于一些元素/选择器。
After a few more tests it turns out it works on some elements/selectors.
它以某种方式与选择器一起使用: document.getElementById('maindiv')
,其中 maindiv
是一个不是用React.js生成的元素,只是简单的html。然后它返回 true
。
It somehow works with the selector: document.getElementById('maindiv')
, where maindiv
is an element not generated with React.js, and just plain html. Then it returns true
.
但是一旦我尝试选择使用React生成的不同ElementById。 js它返回false。它也不能与 document.body
一起工作,尽管如果我在console.log中它们( getElementsByClassName('bla')它们基本上都返回相同的东西[0]
也不起作用)
But as soon as I try and select a different ElementById that is generated with React.js it returns false. And it wont work with document.body
either, though they all essentially return the same thing if I console.log them (getElementsByClassName('bla')[0]
also doesn't work)
应该有一种简单的方法通过选择节点
,无需诉诸jQuery或其他选择器,我知道它在某处...
There should be a simple way to select the node via this
, without having to resort to jQuery or other selectors, I know it's in there somewhere..
从安装它们的相同DOM元素中卸载组件。所以如果你做了类似的事情:
Unmount components from the same DOM element that you mount them in. So if you did something like:
ReactDOM.render(<SampleComponent />, document.getElementById('container'));
然后你将卸载它:
ReactDOM.unmountComponentAtNode(document.getElementById('container'));
这是一个简单的JSFiddle ,我们安装组件,然后在3秒后卸载它。
Here is a simple JSFiddle where we mount the component and then unmount it after 3 seconds.