【问题标题】:How to avoid reactjs to be included twice in a rails and reactjs environment?如何避免 reactjs 在 rails 和 reactjs 环境中包含两次?
【发布时间】:2015-04-18 15:28:13
【问题描述】:

我正在使用 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.

【问题讨论】:

    标签: ruby-on-rails reactjs tcomb


    【解决方案1】:

    我发现 react 在

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

    当我删除线时

    //= require react-server
    

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

    【讨论】:

      【解决方案2】:

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

      // require react
      // require react-ujs
      

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

      var React = require("react");
      

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

      【讨论】:

        【解决方案3】:
        //= require_self
        //= require react_ujs
        
        global.React = require('react');
        

        这就是我使用 Browserify & Rails 的方式。也可以让我们进行测试。

        【讨论】:

        • 我会尝试,所以你的方式 react_ujs 不会抱怨找不到 React 吗?
        • 很抱歉,它不起作用,除非您将 React 声明为全局变量。否则 react_ujs 会抱怨它找不到 React。未捕获的 ReferenceError:React 未定义
        • 是的。我打错了,应该在全球范围内。上面固定。我一直试图让服务器端渲染工作的另一种方法是预编译/浏览器化并需要 react_ujs 之上的包。仍在努力并设置 Mocha 测试。
        猜你喜欢
        • 2018-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-11
        • 2012-07-01
        • 1970-01-01
        相关资源
        最近更新 更多