6

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

// require react
// require react-ujs

我也通过使用 tcomb-form

var t=require("tcomb-form")

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

我应该如何正确使用带有 Rails 的 reactjs?

编辑:

顺便说一句,如果您在控制台中看到此错误,则可能您遇到了同样的问题,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.
4

3 回答 3

1

我发现 react 在

app/assets/javascripts/server_rendering.js

当我删除线

//= require react-server

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

于 2017-05-22T20:09:27.567 回答
0
//= require_self
//= require react_ujs

global.React = require('react');

这就是我使用 Browserify & Rails 的方式。也为我们的测试做好了准备。

于 2015-05-01T22:16:08.060 回答
0

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

// require react
// require react-ujs

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

var React = require("react");

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

于 2015-04-20T03:00:01.563 回答