Search code examples
ruby-on-railsrubynpmreactjsreact-rails

How can I use this React library with React-Rails


I came upon this:

https://github.com/ssorallen/turbo-react

And I like what it does, I am just a bit confused about how I use this code within my Rails project that currently uses the React-Rails gem

I am mostly confused about where to put the code so that I can read it into my Rails gem with React Rails. If I paste the main JS file into my vendor/assets folder, I get this message:

Uncaught ReferenceError: global is not defined

I know that I am just fundamentally misunderstanding how to include this kind of JS file into my Rails project.


Solution

  • src/turbo-react.js is meant for a Node.js environment, not a browser environment. I see another file, public/dist/turbo-react.min.js, which has been "compiled" to run in a browser (using Webpack). You should copy that file into vendor/assets, then require it in application.js.

    The first few lines of src/turbo-react.js gave me some signals that it is for Node.js:

    if (global.Turbolinks === undefined) {
      throw "Missing Turbolinks dependency. TurboReact requires Turbolinks be included before it.";
    }
    
    var HTMLtoJSX = require("htmltojsx");
    var JSXTransformer = require("react-tools");
    var React = require("react");
    

    global and require are both Node.js concepts which don't exist in the browser.