如何避免reactjs被包含在rails和reactjs环境中两次?

时间:2015-04-18 15:28:14

标签: ruby-on-rails reactjs tcomb

我正在使用react-rails和browserify。在application.js中我使用

// require react
// require react-ujs

我也使用

来使用tcomb-form
var t=require("tcomb-form")

结果我看到reactjs在我的资产管道中被包含两次。

我应该如何正确地将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.

2 个答案:

答案 0 :(得分:1)

我发现反应在

中被引用了两次
app/assets/javascripts/server_rendering.js

当我删除该行

//= require react-server

它解决了我的问题并摆脱了addComponentAsRefTo()错误。 请记住,我还没有尝试过部署应用程序,但这有助于让它变得粗糙。

答案 1 :(得分:0)

顺便说一下,我想出的解决方法是从application.js中删除以下内容

// require react
// require react-ujs

只需在application.js中复制并粘贴react-ujs的源代码并使用

var React = require("react");

我知道它不优雅,如果您有任何更好的建议,请告诉我。