2015-04-18 27 views
6

我正在使用react-rails和browserify。在我的application.js使用如何避免reactjs在rails和reactjs环境中被包含两次?

// require react 
// require react-ujs 

而且我用

var t=require("tcomb-form") 

当我看到reactjs包括在我的资产管道事半功倍的效果使用tcomb形式。

我应该如何正确使用reactjs与rails?

编辑:

顺便说一下,如果你在你的控制台看到此错误,可能是你有得到了包括两次reactjs同样的问题。

[Error] Error: 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. 

回答

0

顺便说一下,我想出了一个解决方法是删除的application.js

// require react 
// require react-ujs 

,只是复制和粘贴在application.js中反应-UJS的源代码和以下使用

var React = require("react"); 

我知道这不是优雅的,如果您有任何更好的建议,请让我知道。

0
//= require_self 
//= require react_ujs 

global.React = require('react'); 

这是我如何使用与Browserify的反应& Rails。也为我们进行测试。

+0

我会尝试,所以你的方式react_ujs不抱怨无法找到React? – ziweizhou

+0

抱歉,它不起作用,除非您将React声明为全局变量。否则react_ujs会抱怨找不到React。 未捕获ReferenceError:未定义反应 – ziweizhou

+0

是的。我输错了它,应该已经在全球范围内了。固定在上面。另一种我一直试图让服务器端渲染工作的方式是预编译/ browserify,并要求上面的react_ujs包。仍在继续努力并开展摩卡测试。 –

1

我发现,发生反应,在

app/assets/javascripts/server_rendering.js 

被引用了两次。当我删除行

//= require react-server 

它修好了我的问题,并摆脱了addComponentAsRefTo()的错误。 请注意,我还没有尝试部署应用程序,但这有助于让它受到伤害。