Converting existing Rails + React application to React on Rails with Server Side Rendering (SSR)

383 views Asked by At

I need to convert at least small part of existing RoR + React + Redux app to React on Rails with Server Side rendering for measure performance and effort.

Already made setup from this tutorial shakacode/react_on_rails_demo_ssr_hmr (previous version). And SSR is working for HelloWorld. But how to pull out needed component and make it work with SSR is a tough call. Following this tutorial with my is no help I a different errors all way long. I tried to render component from my app on /hello_world page from tutorial (with all steps with registering component for SSR) but got this:

 Field 'browser' doesn't contain a valid alias configuration
    using description file: /app/package.json (relative path: ./app/frontend/folder/my_component_server.js)
      no extension
        Field 'browser' doesn't contain a valid alias configuration

Also markup could be confusing because React on Rails needs declared component in .erb template, while I already included React app as following in my haml file:

 %div#root <-- here is attaching react app
   = stylesheet_pack_tag "fonts", "styles"
   = javascript_pack_tag "my_react_app_entry_point"
1

There are 1 answers

1
sectasy On

Just use this gem (https://github.com/reactjs/react-rails)

Everything you need to get starting is in readme. This gem allows you to create React components and use in rails view by simply execute:

<%= react_component("HelloWorld", { greeting: "Hello from react-rails." }) %>