setState导致不变冲突addComponentAsRefTo
setState cause Invariant Violation addComponentAsRefTo
我将react-rails
与Fluxxor
和React
一起使用。只要我停留在同一页面上,我的组件就可以完全发挥作用。
然而,如果我通过点击其他链接来更改页面,并返回到我的组件,当我尝试在其上使用setState
时,它会抛出错误:
Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's `render` method). Try rendering this component inside of a new top-level component which will hold the ref.
我的实际代码可以在这里找到。这里的问题似乎是setState
方法。也许我的Chosen
组件的refs
属性无法重新渲染?可能是Turbolink
引起的?
我认为这是Chosen
组件实现中的一个问题。
当它被赋予新的道具时,React会重新渲染它,并在页面中放置新的节点。然而,$.fn.chosen
已经被实例化,并且它被附加到不再在页面中的DOM节点上。我怀疑,在这一过程中的某个地方,对旧节点和组件的引用被保留了下来,即使在它们被卸载之后也是如此。
我在React中使用select2
时也遇到了同样的问题。我发现Ryan Florence的jQuery+React指南非常有用:
我们需要一种方法来停止使用React进行渲染,执行jQuery对话框,然后再次使用React开始渲染。有些人称之为"门户"。您为React打开了一个门户,跳过一些老派的DOM内容,然后继续在另一边进行。
最大的诀窍是什么都不渲染,然后在组件内部调用React.renderComponent。
var Dialog = React.createClass({ render: function() { // don't render anything, this is where we open the portal return <div/>; }, componentDidMount: function() { var node = this.getDOMNode(); // do the old-school stuff var dialog = $(node).dialog().data('ui-dialog'); // start a new React render tree with our node and the children // passed in from above, this is the other side of the portal. React.renderComponent(<div>{this.props.children}</div>, node): } });
来源https://github.com/ryanflorence/react-training/blob/gh-pages/lessons/05-wrapping-dom-libs.md
相关文章:
- Javascript-ID冲突的几率
- 导致内容安全策略(CSP)冲突错误的本地jquery.js文件
- Pg承诺性能提升:在冲突中
- 数据与Javascript中的继承冲突
- Angular ng控制器与ng应用程序冲突
- 返回按钮代码段的Jquery冲突
- 调试一个简单的jQuery函数;想知道是否与其他代码冲突
- 使用JavaScript和HTML5画布进行冲突检测
- javascript对象原型与jquery冲突
- jQuery与导航菜单上的mouseover事件冲突.
- Javascript滑块不滑动,如何判断是否存在JS冲突
- Bing语音和Bing地图在Windows 8应用商店应用程序中冲突
- Javascript两个日期选择器冲突
- Sequelize:属性之间的命名冲突'播放列表'以及关联'播放列表'
- 2D Processingjs游戏中的冲突
- 为什么'滚动到顶部'脚本导致类型错误?jQuery冲突
- 防止WordPress javascript冲突的良好实践
- JQuery手风琴菜单与mmenu冲突-JQuery 1.4 vs 1.7
- Greasemonkey1.0中的jQuery与使用jQuery的网站冲突
- 如何解决jQuery冲突